Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-04-2012, 09:05 PM   PM User | #1
nikko50
Regular Coder

 
Join Date: May 2004
Posts: 380
Thanks: 63
Thanked 0 Times in 0 Posts
nikko50 is an unknown quantity at this point
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>
nikko50 is offline   Reply With Quote
Old 09-04-2012, 09:15 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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>
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
nikko50 (09-04-2012)
Old 09-05-2012, 12:14 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-05-2012, 12:53 AM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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...
xelawho is offline   Reply With Quote
Old 09-05-2012, 03:38 AM   PM User | #5
nikko50
Regular Coder

 
Join Date: May 2004
Posts: 380
Thanks: 63
Thanked 0 Times in 0 Posts
nikko50 is an unknown quantity at this point
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
nikko50 is offline   Reply With Quote
Old 09-05-2012, 05:06 AM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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>
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
nikko50 (09-05-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:52 PM.


Advertisement
Log in to turn off these ads.