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

Home Forums JavaScript Learning JS Reply To: Learning JS


Ok, how does your script know only to look for an article?

It’s looking for the button’s parent element, which is the article in my demo. Remember when we were talking about what .parentNode does?

If your HTML looks like this:

 <div class='commentbox'>
    <p>Hey, I'm the new user!</p>
    <span class='detail1' data-username='newuser'>newuser</span>

Then to find data-username from the button event, you’d do

var username = 
    // button that was clicked 
    // parent element (the article)
    // child element with data-username attribute
    // "newuser"