Andy
Andy

Reputation: 1432

Button/DIV toggle

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.

Upvotes: 0

Views: 277

Answers (3)

ONOZ
ONOZ

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

user900360
user900360

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

Amritpal Singh
Amritpal Singh

Reputation: 1785

I have chaned the fiddle

Updated Fiddle

Upvotes: 2

Related Questions