Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Get Text of a element

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #37885
    nutt318
    Member

    Hello, I’m trying to figure out how I can get text below ‘My Text Here’ using javascript and cannot figure it out. I have a function called in the first

    and here is the HTML setup.

    Thanks!








    My Text Here

    Code to try and get text.



    #102156
    Mottie
    Member

    Try this instead… it’s more direct (demo)

    this.parentNode.parentNode.getElementsByTagName('span')[0].title

    The problem with the code above is that the first child node isn’t the span, it’s a text node from the tab to format the HTML. So changing that to

    this.parentNode.nextElementSibling.nextElementSibling.nextElementSibling.nextElementSibling.nextElementSibling.childNodes[1].title

    would also work, but it’s much more rigid in that if you change the number of table cells it will break.

    #102157
    nutt318
    Member

    Thank you Mottie!

    I did have to change the 0 to a 1 to get it to work, not sure why.


    this.parentNode.parentNode.getElementsByTagName('span')[1].title
    #102158
    Mottie
    Member

    I didn’t see your full HTML, but most likely there is more than one span in that table row.

    #102177
    nutt318
    Member

    Not sure why but it doesnt like IE, I’ve tried it on firefox and chrome and it works great.

    Any ideas on why IE doesnt work?

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.