Increment a text field value So let's take this code which increments a Javascript text field value:

&lt;div id="counter"&gt;1&lt;/div&gt;
var counter = $(".counter")
counter.html( parseInt(counter.html()) + 1 )


It can be made shorter by using the Unary operator + which behaves like Number():

&lt;div id="counter"&gt;1&lt;/div&gt;
var counter = $(".counter")
counter.html( +counter.html() + 1 )


is there a shorter and better way to do this?

I tried combinations of the ones below, neither work:

&lt;div id="counter"&gt;1&lt;/div&gt;
$(".counter").html( +this.innerHTML + 1 )


and

&lt;div id="counter"&gt;1&lt;/div&gt;
$(".counter").html( +this.value + 1 )


and

&lt;div id="counter"&gt;1&lt;/div&gt;
$(".counter").html( +$this.html() + 1 )
Not a question that fits CR well. This is clearly short, untested sample code. You need $("#counter"), not $(".counter").



var counter = $("#counter")
counter.html( parseInt(counter.html()) + 1 )
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
&lt;div id="counter"&gt;1&lt;/div&gt;




If you truly wish to increase the content of a field that you access by id you could try this:



document.getElementById('counter').textContent++;
&lt;div id="counter"&gt;1&lt;/div&gt;




This works as of IE9.. Personally I would always make sure that there is a number in textContent since otherwise the value will become NaN which looks like amateur hour.