Toggle font awesome class on button click

后端 未结 7 610
心在旅途
心在旅途 2020-12-31 22:37

I have the code as here in this jsfiddle, I want the font awesome icon to change on button click using javascript, but it does\'nt seem to work. I\'m new to javascript so pl

相关标签:
7条回答
  • 2020-12-31 23:08

    .toggleClass() is a jQuery function and you're using it as JavaScript. Try this:

    $("#favIcon").toggleClass('fa-star-o fa-star');
    
    0 讨论(0)
  • 2020-12-31 23:12

    Additionally with what Difster said, .toggleClass is a jQuery function.

    Beyond that, I wouldn't use the DOM to define bindings to functions; Using jQuery's event listener system will allow for more maintainable and understandable code:

    https://jsfiddle.net/0n1n9o9n/2/

    $(document).ready(function() {
      $('#favBtn').on('click', function() {
        $("#favIcon").toggleClass('fa-star-o fa-star');
      });
    });
    
    0 讨论(0)
  • 2020-12-31 23:16

    I would split my comment into multiple remarks:

    1/ As mentioned in the other comments: $("#favIcon").toggleClass('fa-star-o fa-star'); This is a mix of JS and JQuery calls. If you want to use pure JS you would use:

    document.getElementById("favIcon").classList.toggle('fa-star-o');
    

    If you want to use JQuery you can use () As mentioned in Difster's comment:

    $("#favIcon").toggleClass('fa-star-o');
    

    2/ As mentioned already in the comments, it's better to attach an event listener.

    Your Fiddle js would look like this:

    document.getElementById("favBtn").addEventListener("click", fav);
    function fav() {
        document.getElementById("favIcon").classList.toggle('fa-star-o');
        document.getElementById("favIcon").classList.toggle('fa-star');
    }
    

    And remove the "onClick" on the HTML since you would be attaching a js event listener.

    Links to check: JQuery toggleClass - js classList

    0 讨论(0)
  • 2020-12-31 23:20

    Difster's response is correct. Here is how you can accomplish the same thing using native JavaScript:

    document.getElementById("favIcon").classList.toggle('fa-star-o');
    document.getElementById("favIcon").classList.toggle('fa-star');
    
    0 讨论(0)
  • 2020-12-31 23:26

    When using jQuery you never need to use an inline attribute eventHandler.

    onclick=

    • Demo 1 uses jQuery .toggleClass()

    • Demo 2 uses JavaScript .classList.toggle()

    • Demo 3 uses CSS :checked pseudo-class

    Update v4 to v5: Go to Start | Font Awesome. There are some class changes as well. See Demo 4.

    Demo 1 -- jQuery

    $('button').on('click', fav);
    
    function fav(e) {
      $(this).find('.fa').toggleClass('fa-star-o fa-star');
    }
    :root {
      font: 400 16px/1.5 Verdana;
    }
    
    button {
      display: inline-block;
      font: inherit;
      padding: 0px 5px;
      cursor: pointer;
    }
    
    button::after {
      content: ' Favorite'
    }
    <link href='https://cdn.jsdelivr.net/fontawesome/4.7.0/css/font-awesome.min.css' rel='stylesheet'>
    
    <button>
        <i class="fa fa-star-o"></i>
    </button>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    Demo 2 -- Plain JavaScript

    document.querySelector('button').addEventListener('click', fav);
    
    function fav(e) {
      const tgt = e.target.firstElementChild;
      tgt.classList.toggle('fa-star');
      tgt.classList.toggle('fa-star-o');
    }
    :root {
      font: 400 16px/1.5 Verdana;
    }
    
    button {
      display: inline-block;
      font: inherit;
      padding: 0px 5px;
      cursor: pointer;
    }
    
    button::after {
      content: ' Favorite'
    }
    <link href='https://cdn.jsdelivr.net/fontawesome/4.7.0/css/font-awesome.min.css' rel='stylesheet'>
    
    <button>
      <i class="fa fa-star-o"></i>
    </button>

    Demo 3 -- Pure CSS

    :root {
      font: 400 16px/1.5 Verdana;
    }
    
    #fav {
      display: none
    }
    
    #fav+label {
      display: inline-block;
      border: 2px outset grey;
      padding: 0px 5px;
      cursor: pointer;
      -webkit-appearance: button;
      -moz-appearance: button;
      appearance: button;
    }
    
    #fav+label::after {
      content: ' Favorite'
    }
    
    #fav+label>.fa-star-o {
      display: inline-block
    }
    
    #fav+label>.fa-star {
      display: none;
    }
    
    #fav:checked+label>.fa-star-o {
      display: none;
    }
    
    #fav:checked+label>.fa-star {
      display: inline-block
    }
    <link href='https://cdn.jsdelivr.net/fontawesome/4.7.0/css/font-awesome.min.css' rel='stylesheet'>
    
    <input id='fav' type='checkbox'>
    <label for='fav'>
      <i class="fa fa-star-o"></i>
      <i class="fa fa-star"></i>
    </label>

    Demo 4 -- Font Awesome 5

    jQuery / JavaScript / CSS

    /* #1 jQuery */
    $('button.jq').on('click', jQFav);
    function jQFav(e) {
      $(this).find('.fa-star').toggleClass('fas far');
    }
    
    /* #2 JavaScript */
    document.querySelector('button.js').addEventListener('click', JSFav);
    function JSFav(e) {
      const tgt = e.target.firstElementChild;
      tgt.classList.toggle('far');
      tgt.classList.toggle('fas');
    }
    /* #1 JS / #2 jQ */
    :root {
      font: 400 16px/1.5 Verdana;
    }
    
    button {
      display: inline-block;
      font: inherit;
      padding: 0px 5px;
      cursor: pointer;
    }
    
    button::after {
      content: ' Favorite'
    }
    
    /* #3 CSS */
    
    #fav {
      display: none
    }
    
    #fav+label {
      display:inline-block;
      border: 2px outset grey;
      padding: 0px 5px;
      cursor: pointer;
      -webkit-appearance: button;
      -moz-appearance: button;
      appearance: button;
    }
    
    #fav+label::after {
      content: ' Favorite'
    }
    
    #fav+label>.far {
      display: inline-block;
    }
    
    #fav+label>.fas {
      display: none;
    }
    
    #fav:checked+label>.far {
      display: none;
    }
    
    #fav:checked+label>.fas {
      display: inline-block
    }
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" crossorigin="anonymous">
    
    <ol>
    <li><fieldset>
    <legend>jQuery</legend>
    
    <button class='jq'>
      <i class='fa-star far'></i>
    </button>
    
    </fieldset></li>
    
    <li><fieldset>
    <legend>Plain JavaScript</legend>
    
    <button class='js'>
      <i class='fa-star far'></i>
    </button>
    
    </fieldset></li>
    
    <li><fieldset>
    <legend>Pure CSS</legend>
    
    <input id='fav' type='checkbox'>
    <label for='fav'>
      <i class="fa-star far"></i>
      <i class="fa-star fas"></i>
    </label>
    
    </fieldset></li>
    </ol>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    0 讨论(0)
  • 2020-12-31 23:27

    Hope It Helps

    $('.fa-star').click(function() {
      $(this).toggleClass('fas far');
    })
     <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
    
    
    <script src="https://unpkg.com/jquery/dist/jquery.min.js"></script>
    
     
     
     
     
     
     <i class="far fa-star"></i>

    0 讨论(0)
提交回复
热议问题