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

JavaScript AJAX Basics (retiring) AJAX and APIs Making the AJAX Request

Dongyun Cho
Dongyun Cho
2,256 Points

$(this).siblings().removeClass('selected'); why this code doesn't work?

'''javascript $(document).ready(function(){ $('button').click(function(){ $(this).siblings().removeClass('selected'); $(this).addClass('selected'); }); }); ''' I used siblings() method for $('button').removeClass('selected') in teacher's code. But why this code doesn't work?

2 Answers

Steven Parker
Steven Parker
229,732 Points

It may have something to do with the other code you use it with.

I tried it using some HTML and CSS I whipped up for testing it and it seems to work just fine:

code.html
<button>one</button>
<button>two</button>
<button>three</button>
script.css
.selected { color: red; }

You had the right idea about code formatting but the wrong character. You need three accents (`) not apostrophes ('). And please post the HTML part of the code, I suspect that's where the issue is.

Kamil Żogło
Kamil Żogło
10,114 Points

It doesn't work, because <button> elements aren't siblings. Every button is embedded in different <li> tag. Solution is to change HTML code. When you put all buttons inside ONLY one <li> tag your code will work properly.

Steven Parker
Steven Parker
229,732 Points

I don't think Dongyun has been a student for a couple of years, but your guess about his code seems reasonable and perhaps this will benefit other readers.