Luke
Luke

Reputation: 2400

Changing image while clicking/unclicking button

Can somebody tell me how to dynamically change the image of a button while clicking it?

My code:

$('.gamebox_minimap_plus').click(function() { $(this).css("background-image","url('gfx/plus2.png')"); });

It works after a click, not while clicking, and does not come back to plus1.png after I stop clicking.

Upvotes: 2

Views: 736

Answers (2)

ArVan
ArVan

Reputation: 4275

Use mousedown and mouseup insead of click. Like this:

$('.gamebox_minimap_plus').mousedown(function() { $(this).css("background-        image","url('gfx/plus2.png')"); });
$('.gamebox_minimap_plus').mouseup(function() { $(this).css("background-image","url('gfx/plus1.png')"); });

Upvotes: 1

John Hartsock
John Hartsock

Reputation: 86872

I think you want to use the mousedown and mouseup events.

jQuery .mousedown()

jQuery .mouseup()

In addition if you are using jQuery 1.7 or higher you should begin using/getting use to the on() and off() methods.

Upvotes: 4

Related Questions