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
Fernando Jimenez
Courses Plus Student 8,455 PointsHow do I keep Jquery from selecting an element as an array and instead select the element right next to it.
<div class="row">
<div class="col-md-2"></div>
<div id="homeFAQ" class="col-md-8">
<h2>Frequently Asked Question</h2>
<div>How much will hiring an attorney cost?</div>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </li>
<div>How much will hiring an attorney cost?</div>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </li>
<div>How much will hiring an attorney cost?</div>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </li>
<div>How much will hiring an attorney cost?</div>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </li>
</div>
<div class="col-md-2"></div>
</div>
jQuery( 'div#homeFAQ div' ).click(function(e) {
e.preventDefault();
// Select the li next to the div. (just ONE though not an array of li's)
var current = jQuery( 'div#homeFAQ div' ).next();
//display what the current variable returns.
console.log(current);
});
so when I run this code it returns an array of elements so if I was to toggle() it will toggle all the (div#homeFAQ div li) elements. Hopefully I am explaining things right.
1 Answer
Steven Parker
243,656 PointsThe selector "#homeFAQ div" will select every "div" element that is inside the one with the id of "homeFAQ". In the current code, this is a collection of 4 items.
Then, if you add a "next()" function, it will get the next element after each of those 4 div's. In this code, that will be a collection of 4 list items.
If you want only the one list item after the div you clicked on, you can use "e.target" to identify the clicked div, and then apply the "next" function to that:
var current = jQuery(e.target).next();