Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial:) :((
6,776 PointsI need help on this challenge
i don't understand why my code is not running , thanks.
var list = document.getElementsByTagName('ul')[0];
var p = document.getElementsByTagName('p');
list.addEventListener('click', function(e) {
if (e.target.tagName == 'BUTTON') {
let li = e.target.parentNode;
let prevP = p.previousElementSibling;
let ul = li.parentNode;
ul.insertBefore(li, prevP);
}
});
<!DOCTYPE html>
<html>
<head>
<title>JavaScript and the DOM</title>
</head>
<link rel="stylesheet" href="style.css" />
<body>
<section>
<h1>Making a Webpage Interactive</h1>
<p>Things to Learn</p>
<ul>
<li><p>Element Selection</p><button>Highlight</button></li>
<li><p>Events</p><button>Highlight</button></li>
<li><p>Event Listening</p><button>Highlight</button></li>
<li><p>DOM Traversal</p><button>Highlight</button></li>
</ul>
</section>
<script src="app.js"></script>
</body>
</html>
2 Answers
Steven Parker
231,271 PointsWhoa ... you're going a bit overboard here. Here's a few hints:
- you won't need to define any additional globals
- you can navigate directly from the button (
e.target
) to the paragraph - you won't need to reference the parent element
- you don't need to add any new elements
- but you do need to set the class on the paragraph
- this task can be done in one single statement
:) :((
6,776 Points"'js var list = document.getElementsByTagName('ul')[0];
var p = document.querySelector("p.highlight"); list.addEventListener('click', function(e) { if (e.target.tagName == 'BUTTON') { let li = e.target.parentNode; let prevP = p.previousElementSibling; let ul = li.parentNode; ul.insertBefore(li, prevP); } });"' "'html ><li><p class="highlight">Element Selection</p><button>Highlight</button></li> <li><p >Events</p><button>Highlight</button></li> <li><p >Event Listening</p><button>Highlight</button></li> <li><p >DOM Traversal</p><button>Highlight</button></li> "'
I tried putting a class to a paragraph , still doesn't work, i tried list.chidren and document.querySelector("list > p") but still the same
Steven Parker
231,271 PointsIt's hard to tell without formatting, but it looks like you went back to the original code — all of my original hints still apply to that code.
And the Markdown formatting marks for code are 3 accents (```), not 3 apostrophes (''').
:) :((
6,776 Points:) :((
6,776 PointsCan you please show an example, so i can understand better?
Steven Parker
231,271 PointsSteven Parker
231,271 PointsTry implementing the hints, then post your modified code if you're still having trouble.
And you might consider giving yourself a slightly more compact user name.
:) :((
6,776 Points:) :((
6,776 Points"' js var list = document.getElementsByTagName('ul')[0]; list.addEventListener('click', function(e) { if (e.target.tagName == 'BUTTON') { document.querySelector('p').className= "highlight";
} });"" i give up , i really can't understand and i did the whole chapter again and looked over mdn. :c
Steven Parker
231,271 PointsSteven Parker
231,271 PointsThe problem with "
document.querySelector('p')
" is that it will always select the first paragraph on the page, which may not be the one which is a sibling of the pressed button. So it is important to use DOM traversal to find the correct paragraph.You had the right idea before to use "
previousElementSibling
", it just needs to be employed in a different way.