简体   繁体   中英

How to change an empty star glyphicon button to star glyphicon button onclick with JS?

This seems to change the glyphicon only when I click glyphicon itself. I want it to change when I click the button.

<script>       
      $('.glyphicon').click(function(){
$(this).toggleClass('glyphicon-star-empty glyphicon-star');
});     
    </script>

<button class="btn btn-default">
    <span class="glyphicon glyphicon-star-empty"></span>
  </button>

You can use this jQuery code.

$(document).ready( function() {
    $('.btn').click(function(){
        $(this).find('.glyphicon').toggleClass('glyphicon-star-empty glyphicon-star');
    });     
})

You have to bind the event on the button, not only the icon, else it will be triggered only clicking the star.

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