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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Can't get getElementsByTagName() to work

    I'm trying to do a proof of concept and I'm having difficulty. I've reduced the code to the bare minimum.

    I'm expecting 3 alerts indicating the id's of the 3 <p>'s in the html page. I'm getting the same message all 3 times:
    "I am 0 test_p1"
    "I am 1 test_p1"
    "I am 2 test_p1"

    Here is the code:

    <body>
    <p id="test_p1" m="testme">This is some text</p>
    <p id="test_p2" m="xxx">This is some text</p>
    <p id="test_p3" m="testme">This is some text</p>

    <script>

    function do_test () {
    var i=0;

    var e = document.getElementsByTagName("p");

    for (i=0; i<e.length; i++) {
    alert("I am "+i+" "+e[i].id);
    }
    }

    do_test();

    </script>
    </body>
    </html>

  • #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
    Could you provide a link instead?

    The snippet you've shown works/alerts fine.
    Learn how to javascript at 02geek

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

  • #3
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Doh!

    I think I must have done something stupid. When I moved it to a openly accessible directory of my webspace, it worked.... I think I can fix it from here. Thanks for the help.


  • #4
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ...but not in a frame...

    Well, the code does work when it's in a normal page, but when I put the page in a <frame>, it breaks. I know frames are frowned upon, but I have to do it this way. Can anyone think of a reason the SAME FILE doesn't work in a frame?

    TIA,


  •  

    Posting Permissions

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