PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 8, 2022

[FIXED] How to get DIV sub-elements using javascript

 September 08, 2022     adsense, ajax, html, javascript, jquery     No comments   

Issue

HTML Code

<ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-1714167228247329" data-ad-slot="8611448539" data-adsbygoogle-status="done" data-ad-status="unfilled">

I have checked : https://www.w3schools.com/js/js_htmldom_elements.asp and tried but it not help.

I want to take this element data-ad-status="unfilled" from HTML using javascript. So, i can use it in if else statement.

Like

if (data-ad-status="unfilled") {
some fuctions; 
}

Solution

  • As you told me that data is coming from async way I have added setTimeOut() function to wait for around 2 seconds here.
  • I do not have access to how data is coming so this time for 2s is hard coded. change it's value and try to run it. and I am sure it will solve your problem.
  • But use asyn-await function and make selectAd() wait till you data get loaded.

const selecteAd = () => {
  const myDiv = document.querySelector("[data-ad-status='unfilled']");
  const selectAllWithAttributeAdStatus = document.querySelectorAll("[data-ad-status]");
  /*This(selectAllWithAttributeAdStatus) one is just for showing how to select elements with data-ad-status attribute */
  //let adStatus = myDiv.getAttribute("data-ad-status");
  selectAllWithAttributeAdStatus.forEach(ad => {
    if ((ad.getAttribute("data-ad-status")) === "unfilled") {
      ad.style.background = "red";
      ad.style.height = "100px";
      ad.style.width = "100px";
      ad.style.display = "inline-block";
    } else {
      ad.style.background = "green";
      ad.style.height = "100px";
      ad.style.width = "100px";
      ad.style.display = "inline-block";
    };
  })
}
const myTimeout = setTimeout(selecteAd, 2000);
<div class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-1714167228247329" data-ad-slot="8611448539" data-adsbygoogle-status="done" data-ad-status="unfilled"></div>
<div class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-1714167228247329" data-ad-slot="8611448539" data-adsbygoogle-status="done" data-ad-status="somethingelse"></div>



Answered By - Nikkkshit
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing