I just began learning and coding JavaScript and wrote the following. This code will allow the user to input a desired number into a textbox and then increment it or decrement it using the forum + or – buttons. They can also click on the other forum buttons to modify the current number.
All the operators are able to modify the number entered in the textbox except the + operator. It is the exact same code as the others except it uses the + operator instead. Any suggestions why this + operator does seem to work?
Below is the code:
<html>
<head>
<script type=”text/javascript”>
sum=0
adden=0
function add(objId)
{
adden+=1;
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}
function sub(objId)
{
adden=adden-1;
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}
function times2(objId)
{
adden=adden*2;
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}
function times50(objId)
{
adden=adden
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}
function div2(objId)
{
adden=adden/2;
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}
function clr(objId)
{
adden=0;
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}
function inputValue(objId)
{
val=(document.getElementById(“input”).value)
adden=val;
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}
</script>
</head>
<body>
<form>
<input type=”button” value=”+” onclick=”add(‘ans’)” >
<input type=”button” value=”-” onclick=”sub(‘ans’)” >
<input type=”button” value=”x2″ onclick=”times2(‘ans’)” >
<input type=”button” value=”x50″ onclick=”times50(‘ans’)” >
<input type=”button” value=”/2″ onclick=”div2(‘ans’)” >
<input type=”button” value=”clear” onclick=”clr(‘ans’)” >
<input type=”text” id=”input” value=”” />
<input type=”button” onclick=”inputValue(‘ans’)” value=”input” />
</form>
<div id=”ans”>0</div>
</body>
</html>