MoonDarius
MoonDarius

Reputation: 61

jQuery change on click and if i click again change back

i have here an on/off button,

    $(document).ready(function(){
       $("#onOffButton").on("click", function(){
         $("#onOffButton").text("ON");
       });
    })

When i click back, i want to change the button to Off.

Upvotes: 0

Views: 551

Answers (1)

Majed Badawi
Majed Badawi

Reputation: 28414

Try this:

$(document).ready(function(){
       $("#onOffButton").on("click", function(){
         var txt = $(this).text();
         $(this).text(txt=="ON"?"OFF":"ON");
       });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="onOffButton">OFF</button>

Upvotes: 2

Related Questions