I have 3 buttons. I’m trying to get it so that when one is clicked it gets a class of ‘this-button’ and the class is removed from others.
<button id="button1">Button1</button>
<button id="button2">Button2</button>
<button id="button3">Button3</button>
So here’s my simple event function:
$('button').live("click", function (){
$(this).addClass("this-button").siblings().removeClass("this-button");
});
This does not work as expected.
Here’s to try: check the classes in firebug.
http://jsbin.com/orifo/edit
I’m really very confused. I think it’s clear that $(this) refers to all the buttons, rather than just the one latest clicked. Why is this? Shouldn’t it be the one latest clicked?
Can someone explain why this doesn’t work.
Thanks.
Yes, it is the one clicked. Works fine for me. Here’s the exact snippet I tested.