DOM Calculator

Example 1

In this example we use the dom to access elements, and then use the eval function to evaluate a user-entered formula

Simply type in a string such as 32 + 98 * 6 and click GO.

COMPUTE :   

Code:

<script>
	var answer;

	function init(){   
	  answer=document.getElementById("ans").firstChild;
	}

	function comp(id){   
		var res;
		var el = document.getElementById(id);
		try { 
		  if ( el.value != "" ){  
			 res = eval(el.value); 
			 answer.data = res;
		  }
		}catch (e) {  
		   alert("Cannot evaluate " + el.value);  
		   el.focus();
		   return; 
		}
	}
	</script>

Output:

And the answer is: 48