Should mention: I'm not the most experienced web-coder. Nevertheless: Currently dealing with some frontend stuff.
Yesterday I programmed some JavaScript and the results wasn't as I had expected.
I made this demo:
Every paragraph contains a headline and some text. The paragraphs are enclosed within a div-container.
<div class="container">
<p class="paragraph">
<h3 class="headline">Some headline ...</h3>
<span>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. </span>
</p>
<p class="paragraph">
<h3 class="headline">Another headline ...</h3>
<span>Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus.</span>
</p>
<!-- More paragraph-container ... -->
</div>
Let's say I have a reference to the first headline and would like to get the class-name of the first paragraph-tag.
I would to something like this:
// Get a reference to the first headline element.
var headline = document.getElementsByClassName('headline')[0];
// Access the parent of the headline element.
console.log(headline.parentNode.className);
// Result is: container
I expect as return 'paragraph' because the paragraph encloses the headline. Instead I get the class-name of the div.
Can anyone explain this behaviour?