Reputation: 1432
I have a button, the label of which I want to change back and forth as it is clicked. I found this thread
How do you create a toggle button?
and this example
which is great but I'm trying to figure out how to actually change the label (say, from "up" to "down" when it is clicked).
Would appreciate any help.
Upvotes: 0
Views: 277
Reputation: 1410
You can check the class by using .hasClass();
if ($(this).hasClass("down"))
{
$(this).html("Down!");
}
else
{
$(this).html("Up!");
}
In your code: http://jsfiddle.net/Before/XNQce/2/
Upvotes: 2
Reputation:
That's simple.
$(document).ready(function(){
var i=0;
$('a#button').click(function(){
$(this).toggleClass("down");
if(i%2==0)$(this).html('divide by 2');
else $(this).html('not divide by 2');
i++;
});
});
using a variable you can check what state is your button in. Above code is just a crude of that type of state management.
check this http://jsfiddle.net/LmULE/100/
Upvotes: 1