Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts

    Find div with now innerhtml???

    Hey dudes!

    If I have 5 div's like below and am placing innerhtml in some of them how can I find the div id where there is not innerhtml so I may use it. I would like to get the first id that there is no html or text in there so in the code below id="layer2" would be the first empty div.


    Code:
    <div id=layer1 style="visibility:hidden; position: absolute; top: 250; left: 20; width: 550; height: 100;  background-color: #FFFFFF; border: 2px dotted #6699FF; padding: 5px">TEST TEXT</div>
    <div id=layer2 style="visibility:hidden; position: absolute; top: 360; left: 20; width: 550; height: 100;  background-color: #FFFFFF; border: 2px dotted #6699FF; padding: 5px"></div>
    <div id=layer3 style="visibility:hidden; position: absolute; top: 470; left: 20; width: 550; height: 100;  background-color: #FFFFFF; border: 2px dotted #6699FF; padding: 5px">TEST TEXT</div>
    <div id=layer4 style="visibility:hidden; position: absolute; top: 580; left: 20; width: 550; height: 100;  background-color: #FFFFFF; border: 2px dotted #6699FF; padding: 5px">TEST TEXT</div>
    <div id=layer5 style="visibility:hidden; position: absolute; top: 690; left: 20; width: 550; height: 100;  background-color: #FFFFFF; border: 2px dotted #6699FF; padding: 5px"></div>

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    Code:
    <script type="text/javascript">
    var divs=document.getElementsByTagName("div")
    
    for (var i = 0; i < divs.length; i++) {
    if (divs[i].innerHTML==""){
    alert(divs[i].id+" is empty")
    	}
    }
    </script>

  • Users who have thanked xelawho for this post:

    nikko50 (09-04-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    Okay, where did the Japanese at the bottom of the Atari logo come from Xelawho? Is somebody actually trying to resurrect the Atari brand?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    Wait... what? Did Atari die? Oh, man... and I only just updated my avatar

    seriously, though - atari seems to be kicking (twitching?) still, and you can even get the classics for mobile and iPad (please don't take this to mean that I knew this before this afternoon). You should check it out - maybe some bits of your code are still rumbling around out there...

  • #5
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts
    Thanks for the code. One question I have is what if I just want to loop through only the div that start "layer". I have another div on the page but it doesn't start with layer. The only layer ones I have is the ones I posted.

    Tracy

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    Code:
    <script type="text/javascript">
    var divs=document.getElementsByTagName("div")
    
    for (var i = 0; i < divs.length; i++) {
    if (divs[i].innerHTML=="" && divs[i].id.match("layer")){
    alert(divs[i].id+" is empty")
    	}
    }
    </script>

  • Users who have thanked xelawho for this post:

    nikko50 (09-05-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •