So let's take this code which increments a Javascript text field value:
<div id="counter">1</div>
var counter = $(".counter")
counter.html( parseInt(counter.html()) + 1 )
It can be made shorter by using the Unary operator +
which behaves like Number()
:
<div id="counter">1</div>
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:
<div id="counter">1</div>
$(".counter").html( +this.innerHTML + 1 )
and
<div id="counter">1</div>
$(".counter").html( +this.value + 1 )
and
<div id="counter">1</div>
$(".counter").html( +$this.html() + 1 )