0

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

http://jsfiddle.net/LmULE/

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.

Community
  • 1
  • 1
Andy
  • 1,422
  • 5
  • 27
  • 43

3 Answers3

2

I have chaned the fiddle

Updated Fiddle

Amritpal Singh
  • 1,765
  • 1
  • 13
  • 20
2

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/

ONOZ
  • 1,390
  • 2
  • 11
  • 19
1

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/