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
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts

    Denoting current position in doc without using ID attribute?

    I was wondering, let say I have the following HTML fragment:

    Code:
    <b>this is some text<b>
    <span>This is some more text<span>
    
    <script>
    //my script here
    </script>
    and inside the script above, I wish to gain access to the elements and text above. Is it possible to do so without having to explicitly denote an ID attribute somewhere to use as the starting pointing? Something like:

    currentposition.previousSibling //should return <span> tag

    Thanks,
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Not easily.

    Your first line in the <script> tag could be something like:

    //identifier

    Then you can iterate through the document.getElementsByTagName('script') HTMLCollection until you find one with firstChild.nodeValue.indexOf('//identifier') == 0, but that seems a little inefficient.

    You could always give your <script> an id, but you seem to want otherwise.

  • #3
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    Thanks jkd. It just occurred to me that a good compromise would be to give the SCRIPT tag an ID attribute. I don't have physical access to any of the HTML codes above, FYI.

    Great minds think alike I guess
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.


  •  

    Posting Permissions

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