Home > front end >  Swap text using jquery
Swap text using jquery

Time:08-17

I am new in using webpack. I wanted to be able to change the label of button on click from "NO" to "YES" and vice versa when the button itself is clicked. This is what I wanted to achieve: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_toggle_text

import $ from "jquery";

const rootApp = document.getElementById("root");
rootApp.innerHTML = '<button>NO</button>';

CodePudding user response:

If you want to do it with jQuery, you can use click()

$('#root').click(function() {
  var currentState = $(this).text();
  $(this).text((currentState == 'NO' ? 'YES' : 'NO'));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="root">NO</button>

  • Related