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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    311
    Thanks
    17
    Thanked 0 Times in 0 Posts

    getElementsByTagName('p') CONTAINED IN DIV?

    In testament to my search skills (to say nothing of my prowess as a coder)
    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?

    PHP Code:
    var p=document.DIV_NAME.getElementsByTagName('p');//which is silly I know but...
    for (i=0ip.lengthi++){
    bla bla 

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    You are on the right track
    Code:
    <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
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    fuzzy1 (05-23-2008)

  • #3
    Regular Coder
    Join Date
    Apr 2006
    Posts
    311
    Thanks
    17
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

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