Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Selectable with Jquery

Tags:

jquery

css

I have just 3 divs all with a background color of blue.

I need jQuery to make each box selectable.
When the box is clicked/selected, jQuery should add class .selected with a different background color to red. All the others not selected should change back to blue when clicking another div.

Here's the css

.block {
    width: 300px;
    padding: 10px;
    background: blue;
    color:white;
    margin-top:5px;
}

.selectable {
    background: red;
}

Here's the html

<div class="block">
    Some stuff here
</div>

<div class="block">
    Some stuff here
</div>

<div class="block">
    Some stuff here
</div>

​How do I do it with jQuery?
Here's all the actual jsfiddle

like image 974
wolverene Avatar asked Jul 28 '26 02:07

wolverene


1 Answers

Try this jsFiddle example.

$('.block').click(function() {
    $('.block').removeClass('selectable');
    $(this).addClass('selectable');
});​
like image 168
j08691 Avatar answered Jul 30 '26 22:07

j08691



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!