Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have single textbox. when buttons are clicked button value are displayed in textbox. What i want is to store value of textbox in some variable so that i can add these values later.

Here is my code:

XML
<html>
<head>
<title>
</title>
<script>
 window.onload = function() {
    document.getElementById('btnone').addEventListener('click', function(){
        document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnone').value;
    });
       document.getElementById('btntwo').addEventListener('click', function(){
        document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btntwo').value;
    });
    document.getElementById('btnthree').addEventListener('click', function(){
        document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnthree').value;
    });
    document.getElementById('btnfour').addEventListener('click', function(){
        document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnfour').value;
    });
    document.getElementById('btnadd').addEventListener('click', function(){
        document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnadd').value;
    });
}


</script>
</head>
<body>
<input type="button" name="btnone" id="btnone" value="1" onclick="setText1()"  />
 <input type="button" name="btntwo" id="btntwo" value="2" onclick="setText2()"  />
<input type="button" name="btnthree" id="btnthree" value="3" onclick="setText3()"  />
<input type="button" name="btnfour" id="btnfour" value="4" onclick="setText4()"  />
<br/>
<input type="button" name="btnadd" id="btnadd" value="+" onclick="setText+()"  />
<input type="button" name="btneql" id="btneql" value="=" onclick="setText=()"  />
<input type="text" name="textBox" id="textBox" value=""/>
</body>
</html>
Posted
Comments
Thanks7872 14-Jul-15 3:31am    
And what is the problem? Such things can be found on Google very easily. Don't you think so?

var txtvalue=document.getElementById('textBox').value;
 
Share this answer
 
if you're using jQuery you could also use..

JavaScript
$('#textBox').val();
 
Share this answer
 
v3
Comments
Richard Deeming 14-Aug-15 10:24am    
Well, you could, if your intention was to generate a javascript syntax error.

You need to terminate the string constant. You also need to provide a valid jQuery ID selector, starting with the # character. The ID is case-sensitive, so your selector needs to match the case of the ID attribute.
Grant Weatherston 14-Aug-15 10:26am    
my bad i was writing on a mobile device, tricky. fixed it now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900