Bramble
Bramble

Reputation: 1395

How to get css button to stay active after it has been clicked?

Im trying to make a nav bar where once the user clicks on the image, the image stays active. In the following example the leaf would stay green after is is clicked. Here is a bit of code of what Im talking about:

<a class="myButtonLink" href="#LinkURL">Leaf</a>

<style>
 .myButtonLink {
    display: block;
    width: 100px;
    height: 100px;
    background: url('http://kyleschaeffer.com/wordpress/wp-content/uploads/2009/01/buttonleafhover.png') bottom;
    text-indent: -99999px;
 }
 .myButtonLink:hover {
    background-position: 0 0;
 }
</style>

Upvotes: 0

Views: 9441

Answers (2)

Bryan Naegele
Bryan Naegele

Reputation: 660

http://jsfiddle.net/bnaegele/XHBZf/2/

$('.myButtonLink').click(function() {
     $(this).css('background-position', '0 0');
 });

Upvotes: 4

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230306

You could apply a class to it on click.

$('.myButtonLink').click(function() {
    $(this).toggleClass('active');
});

This code has an added effect of deselecting the leaf on a second click. Depending on your requirements, you might want it or not.

Example: http://jsfiddle.net/stulentsev/XHBZf/1/

Upvotes: 1

Related Questions