Add or subtract 1 in jquery

3

I am trying to add or subtract 1 in an input but I do not finish it. the code that I have is this:

onclick="$('#StockPagina').val($('#StockPagina').val()+1).change()"

this changes the value of the input StockPagina but I add a 1 at the end instead of adding one with what as I give it instead of putting 1,2,3,4 puts me 01,011,0111

Can not I just simply add one to the value that is there? Is there anything like the php ++? If there is something type ++ there is also the - to subtract?

Thank you very much for your help.

    
asked by Killpe 14.12.2017 в 21:45
source

2 answers

2

You lack convert the value of your input to number him to run mathematical operations You do this with the Number of JavaScript

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="StockPagina">
<button onclick="$('#StockPagina').val(Number($('#StockPagina').val()) + 1).change()">Sumar</button>
    
answered by 14.12.2017 / 21:51
source
4

Try with:

onclick="$('#StockPagina').val(Number( $('#StockPagina').val() ) + 1).change()"

Remember that the value of input is a string ; we have to convert it into a number before adding; if we do not do it, concatenate.

    
answered by 14.12.2017 в 21:49