jeudi 5 août 2021

Use a checkbox to toggle between 2 numbers

How do I use a checkbox in js/jQuery to toggle just between 2 numbers. I want unchecked to always be $0 and checked to always be $100. The code below comes close. I'm looking to use str.replace as opposed to switching divs using display:none and to output the result in <p id="demo">$0</p>. Thanks

<html>
<body>

<h2>JavaScript String</h2>

<p>The replace() method searches a string for a specified value, or a regular expression,
and returns a new string where the specified values are replaced.</p>

<p id="demo">$0</p>


<input type="checkbox" onclick="myFunction()" value="Try It">
<script>
function myFunction() {
  let str = document.getElementById("demo").innerHTML; 
  
  document.getElementById("demo").innerHTML = str.replace("$0", "$100");
}
</script>

</body>
</html>



Aucun commentaire:

Enregistrer un commentaire