saeed_violinist Posted May 12, 2007 Share Posted May 12, 2007 Hi guys, Im trying to make a simple calculation form...getting two numbera and an operator and calculate the selected operation but no success at all! my code is : <script type="text/javascript"> function operate(){ var number1 = document.getElementById('number1'); var number2 = document.getElementById('number2'); var operator = document.getElementById('operator'); if(number1.value != "") { if(operator.value = "+") { sum = number1.value + number2.value; alert(sum); } else if(operator.value = "-") { sum = number1.value - number2.value; alert(sum); } else if(operator.value = "*") { sum = number1.value * number2.value; alert(sum); }else if(operator.value = "/") { sum = number1.value / number2.value; alert(sum); }else if(operator.value = "%") { sum = number1.value % number2.value; alert(sum); } } else { alert("Please Fill All Fields With Proper Value"); } } </script> <input type='text' id='number1' /> <select name="operator" id="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> <option value="%">%</option> </select> <input type='text' id='number2' /> <input type='button' onclick='operate()' value='GO' /> Quote Link to comment Share on other sites More sharing options...
saeed_violinist Posted May 12, 2007 Author Share Posted May 12, 2007 OK I solved it this maybe useful for other users : <script type="text/javascript"> function operate(){ var x = document.getElementById('x').value; var y = document.getElementById('y').value; var operator = document.getElementById('operator').value; if(x.value != "") { if(operator == "+") { alert(+x + +y); } else if(operator == "-") { alert(+x - +y); } else if(operator == "*") { alert(+x * +y); }else if(operator == "/") { alert(+x / +y); }else if(operator == "%") { alert(+x % +y); } } else { alert("Please Fill All Fields With Proper Value"); } } </script> <input type='text' id='x' /> <select name="operator" id="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> <option value="%">%</option> </select> <input type='text' id='y' /> <input type='button' onclick='operate()' value='GO' /> Quote Link to comment Share on other sites More sharing options...
saeed_violinist Posted May 12, 2007 Author Share Posted May 12, 2007 oh! one problem remained unsolved! the first if statement for checking the forms for empty or filled is not working. can anyone help please? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted May 12, 2007 Share Posted May 12, 2007 you set x as the element's value, then in your if, you check for its value again. you just need to say if(x !== '') Quote Link to comment Share on other sites More sharing options...
saeed_violinist Posted May 13, 2007 Author Share Posted May 13, 2007 thanks man...its now fully working : <script type="text/javascript"> function operate(){ var x = document.getElementById('x').value; var y = document.getElementById('y').value; var operator = document.getElementById('operator').value; if((x != '') && (y != '')) { if(operator == "+") { alert(+x + +y); } else if(operator == "-") { alert(+x - +y); } else if(operator == "*") { alert(+x * +y); }else if(operator == "/") { alert(+x / +y); }else if(operator == "%") { alert(+x % +y); } } else { alert("Please Fill All Fields With Proper Value"); } } function reset() { } </script> <form name="values"> <input type='text' id='x' /> <select name="operator" id="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> <option value="%">%</option> </select> <input type='text' id='y' /> <input type='button' onclick='operate()' value='GO' /> <input type="button" onclick="javascript:document.values.reset();return false" value="NEW"> </form> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.