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

05-23-2008, 04: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

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

<script type="text/javascript">
var obj=document.getElementById('wrap').getElementsByTagName('p'); //This is not silly
for(var i=0;i<obj.length;i++)
<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>
<input type="button" id="button" value="Execute">

Hope this helps :)

05-23-2008, 05: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.