Jump to content

+ sign not adding


alzami

Recommended Posts

the "+" sign is not adding in the div with others.why this happens.other numbers works fine but symbols don't acting like them

<!doctype html><html><head><meta charset="utf-8"><title>Untitled Document</title><style>#inpts{	width:50px;	height:50px;	border:1px solid black;	}</style></head><body><div id="inpts"></div><input type="button" value=" 1 "  onClick="gets('inpts',1);"><input type="button" value=" 2 " onClick="gets('inpts',2);"><input type="button" value=" + " onClick="gets('inpts',+);"><input type="button" value=" = " onClick="gets('inpts',=);"><script>function gets(el,n){var m=document.getElementById(el);m.innerHTML += n;		}</script></body></html>
Link to comment
Share on other sites

Try putting the + symbol and = symbol in quotes like: '+' '='. The reason why it may not work the way you're doing it is because the JavaScript parser is assuming you're trying to use those symbols for some kind of operation, like adding two numbers together using the plus symbol like 2+ 2 and the equal symbol to assign a value to a variable like var car = "BMW";

Link to comment
Share on other sites

The innerHTML property is not a number, it is a string. InnerHTML is always a string. Javascript uses the + operator also for string concatenation, so when one of the operands is a string then it concatenates instead of adds. You need to use a function like parseInt or parseFloat to convert the string to a number, then add.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...