+ Start a Discussion
Money Care 7Money Care 7 

How to add two number based on keyboard entry

Hi All

I have one requirment to add two number.I have created three text field ,two is for number and one is for mathematical operator.when i am entering number and and enter add symbole,the it will be display result.how it is possible

User-added image
Pankaj_GanwaniPankaj_Ganwani
Yes, You can use onKeyup event of javascript on both of your textboxes to achieve this.
Money Care 7Money Care 7
Hi @Pankaj ganwani Could you share some sample for this job?
Pankaj_GanwaniPankaj_Ganwani
Please refer this code:
 
<apex:page >
<apex:form >
    <input type="text" id="a" onkeyup="add();"/>
    <input type="text" id="b" onkeyup="add();"/>
    <input type="text" id="c"/>
</apex:form>

<script>
    function add()
    {
        var aValue = document.getElementById('a').value!='' ? parseInt(document.getElementById('a').value) : 0;
        var bValue = document.getElementById('b').value!='' ? parseInt(document.getElementById('b').value) : 0;
        document.getElementById('c').value = aValue + bValue ;
    }
</script>
</apex:page>

 
Money Care 7Money Care 7
Hi @Pankaj ganwani This is not matching my requirement. my requirement is i have two text field for number.one option will be there if i am input + symbol then it will be add,if i am entering - symbol then it will be substract like this
Pankaj_GanwaniPankaj_Ganwani
Hi,

Please refer below code:
 
<apex:page >
<apex:form >
    <input type="text" id="a" onkeyup="add();"/>
    <input type="text" id="op" onkeyup="add();"/>
    <input type="text" id="b" onkeyup="add();"/>
    <input type="text" id="c"/>
</apex:form>

<script>
    function add()
    {
        var aValue = document.getElementById('a').value!='' ? parseInt(document.getElementById('a').value) : 0;
        var bValue = document.getElementById('b').value!='' ? parseInt(document.getElementById('b').value) : 0;
        
        var opValue = document.getElementById('op').value;
        
        if(opValue!='')
        {
            if(opValue == '+')
                document.getElementById('c').value = aValue + bValue ;
            else if(opValue == '-')
                document.getElementById('c').value = aValue - bValue ;
            else if(opValue == '*')
                 document.getElementById('c').value = aValue * bValue ;
        }
    }
</script>
</apex:page>