0

I have 3 buttons inside div's tages as below.

<div id="make_larger">
<button type="button">Large</button> 
</div>
<div id="make_small">
<button type="button">Smaller</button> 
</div>
<div id="make_normal">
<button type="button">Normal</button> 
</div>

I want when i will click on "Large" button its color should be change to blue(using .selected class of css for it) .Jquery code is as below.

$('#make_larger').bind('click', function () {
        $('body').addClass('large');
        $('body').removeClass('normal');
        $('body').removeClass('smaller');
        $(this).addClass('selected');

     });

But it is not working.Color is not being change. Why?

Any help please.

user1544975
  • 93
  • 2
  • 3
  • 11

3 Answers3

0

change last line to

$(this).children('button').addClass('selected');
0

HTML

<div id="make_larger">
    <button type="button">Large</button>
</div>
<div id="make_small">
    <button type="button">Smaller</button>
</div>
<div id="make_normal">
    <button type="button">Normal</button>
</div>
<style type="text/css">
    .selected{background:blue}
</style>

jQUERY

$('#make_larger').click(function() {
     $('#make_larger button').addClass('selected');
});

DEMO http://jsfiddle.net/mRwMt/10/

TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209
0

you give the containing div the class selected, not the button. See this jsfiddle: http://jsfiddle.net/AFNJ9/.

ofcoure you can do something with css like: .selected button {background:blue;} which will make the background blue of the button.

In my opionion it's nice to use 'on': What's the difference between `on` and `live` or `bind`?

Community
  • 1
  • 1
Tim
  • 9,351
  • 1
  • 32
  • 48