Drop down menu to call functions?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cirustus
    New Member
    • May 2010
    • 4

    #1

    Drop down menu to call functions?

    im trying to create a calculator that adds divides and multiplies either decimal or binary input. i have worked out the 2 functions to do this but i dont know how to call the functions using a pull down menu so when i click calculate it uses the appropriate function.

    i have pasted the code down below any help would be great

    Code:
    <html>
    
    <head>
    <script type="text/javascript">
    function dec(a,b,c)
    
    {
    
    	var a;
    	var b;
    	var c;
    	var answer;
    	
    	a = parseInt(a, 10)
    //	b = parseInt(b, 10)
    	c = parseInt(c, 10)
    	
    	if (b=="+")
    	{
    		answer = a + c
    		document.write(answer)
    	}
    	
    	else if (b=="*")
    	{
    		answer = a * c
    		document.write(answer)
    	}
    	
    	else 
    	{
    		answer = a / c
    		document.write(answer)
    	}
    	
    }
    
    </script>
    
    <script type="text/javascript">
    function bin(a,b,c)
    {
    
    	var a;
    	var b;
    	var c;
    	var answer;
    	
    	a = parseInt(a, 2)
    //	b = parseInt(b, 2)
    	c = parseInt(c, 2)
    	
    	if (b=="+")
    	{
    		answer = a + c
    		document.write(answer.toString(10))
    	}
    	
    	else if (b=="*")
    	{
    		answer = a * c
    		document.write(answer.toString(10))
    	}
    	
    	else 
    	{
    		answer = a / c
    		document.write(answer.toString(10))
    	}
    	
    }
    </script>
    
    
    </head>
    
    <body>
    
    
    <form>
    	<input type="text" name="a" size="2">
    	<p>
    	<input type="text" name="b" size="2">
    	<select name="ab">
    	<option value=""></option>
    	<option value="dec">Decimal</option>
    	<option value="bin">Binary</option>
    	</select>
    	</p>
    	<p>
    	<input type="text" name="c" size="2">
    	</p>
    	<p>
    	<input type="button" value="Calculate" onclick="(a.value, b.value, c.value)">
    	</p>
    </form>
    
    </body>
    
    </html>
    Last edited by acoder; May 6 '10, 10:15 AM. Reason: Please use [code] tags when posting code
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Rather than 2 functions, use one function and pass ab.value to it. Then use that to determine whether to use base 2 or base 10.

    PS. using a, b, c in the global context is incorrect. Use document.getEle mentById() after giving the elements IDs or use document.forms["formName"].a, etc. where "formName" is the name of the form.

    Comment

    Working...