Jquery number increment to <p> tag -


i trying write variable holds main value of paragraph tag seen here:

<button id="button">random words</button>    <p id="inc">0</p>    <script>        $(document).ready(function(){         $('#button').on("click", function(){           var oldval = $("#inc")           $('#inc').text( oldval + 1)         });        });    </script> 

how turn '#inc' number can + 1 increment?

you can use callback text function accept old text , modify new.

you need parse old value integer incrementing it:

$('#button').on("click", function(){       $('#inc').text(function(i,oldval){          return parseint(oldval,10) + 1 ;       });  }); 

working demo


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -