简体   繁体   中英

Changing the value of button when clicked

I am trying to change the color of button when clicked. I don't know what is wrong in this code.

paginationContainer.find("li").on('click', function() {

    var linkNumber = $(this).text();

    $(linkNumber).removeClass('active').addClass('inactive');
    $(this).removeClass('inactive').addClass('active');

});

.active {
    background: yellow
}

.inactive {
    background: red
}

You need to apply the class to the li - not the text, Also you can swapt the classes of active and inactive by using .toggleClasss().

paginationContainer.find("li").on('click', function(){
   $(this).toggleClass('active inactive');
});

are you looking for something like this

HTML

<button class="clickMe">
Button1
</button>
<button class="clickMe">
Button2
</button>
<button class="clickMe">
Button3
</button>
<button class="clickMe">
Button4
</button>

CSS

.clickMe{

  padding:3%;
  background-color:cyan;
  color:white;
}

JS

$('.clickMe').click(function(){
$(this).css('background-color',"green");
});

Working Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM