...

View Full Version : getElementsByTagName('p') CONTAINED IN DIV?



fuzzy1
05-23-2008, 03:44 AM
In testament to my search skills (to say nothing of my prowess as a coder):p
I have been searching for a couple hours now and haven't found any mention of this.
How might one loop through only the elements contained in a div, as one would for elements contained in a form?


var p=document.DIV_NAME.getElementsByTagName('p');//which is silly I know but...
for (i=0; i< p.length; i++){
bla bla

rangana
05-23-2008, 04:05 AM
You are on the right track ;)


<script type="text/javascript">
window.onload=function()
{
var obj=document.getElementById('wrap').getElementsByTagName('p'); //This is not silly
document.getElementById('button').onclick=function()
{
for(var i=0;i<obj.length;i++)
{
obj[i].style.background='#9cf';
obj[i].style.fontWeight='bold';
}
}
}
</script>
<div id="wrap">
<p>This is the content of paragraph 1</p>
<span>This is not a paragraph. This should not change</span>
<p>This is the content of paragraph 2</p>
<p>This is the content of paragraph 3</p>
<p>This is the content of paragraph 4</p>
<p>This is the content of paragraph 5</p>
<p>This is the content of paragraph 6</p>
</div>
<input type="button" id="button" value="Execute">


Hope this helps :)

fuzzy1
05-23-2008, 04:31 AM
Not Silly At All.
Perfect actually!
Just when I start to think I've got a smatterring of a clue...
Thank you very much rangana.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum