Search code examples
jqueryoperatorsaddition

How to make an addition in jquery?


What I'm trying to do:

How can I distinguish the jquery operator (+) which is used to concatenate elements, from the + sign which is used to add elements ?

What I've tried so far

Here, I try to add the values of two inputs. In my code, more complex, the result is the same, it does not add but concatenates the elements..

Here is my code, I can"t see what's wrong here

a=$('#a').val()+'px';
b=$('#b').val()+'px';

c=(a+b);


  $('#result').html(c);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js" integrity="sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
 
<input id="a" type="number" value="10">

<input id="b" type="number" value="10">


<output id="result"></output>


Solution

  • a=$('#a').val(); b=$('#b').val(); 
    c= parseInt(a)+parseInt(b)+'px';    
    $('#result').html(c);
    

    use parseInt to plus