www.webdeveloper.com
Results 1 to 5 of 5

Thread: Arithmetic Operations dropdown(javascript)

  1. #1
    Join Date
    May 2012
    Posts
    3

    Arithmetic Operations dropdown(javascript)

    <html>
    <head>
    <script type="text/javascript">
    function add()
    {
    var a=document.cal.num1.value;
    var b=document.cal.num2.value;
    var c=parseInt(a)+parseInt(b);
    document.cal.res.value=c;
    }

    function sub()
    {
    var a=document.cal.num1.value;
    var b=document.cal.num2.value;
    var c=a-b;
    document.cal.res.value=c;
    }

    function mul()
    {
    var a=document.cal.num1.value;
    var b=document.cal.num2.value;
    var c=a*b;
    document.cal.res.value=c;
    }

    function div()
    {
    var a=document.cal.num1.value;
    var b=document.cal.num2.value;
    var c=a/b;
    document.cal.res.value=c;
    }



    </script>
    </head>


    <body>
    <form name="cal">
    Enter 1st num:<input type="text" name="num1" /> <br/>
    Enter 2nd num:<input type-="text" name="num2" /> <br/>
    Select operation:
    <select name="-please select-">
    <option value="please select">-please select-</option>
    <option value="addition" onclick="add()">addition</option>
    <option value="subtraction" onclick="sub()">subtraction</option>
    <option value="multiplication" onclick="mul()">multiplication</option>
    <option value="division" onclick="div()">division</option>
    </select>

    Result= <input type="text" name="res" />
    </form>
    </body>
    </html>

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    boasting or what?
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Nov 2010
    Posts
    1,085
    maybe asking why it doesn't work in IE or Chrome?

    Code:
    <select name="-please select-" onchange="if( this.value ){ window[ this.value ]() }">
    <option >-please select-</option>
    <option value="add">addition</option>
    <option value="sub">subtraction</option>
    <option value="mul">multiplication</option>
    <option value="div">division</option>
    </select>

  4. #4
    Join Date
    Nov 2010
    Posts
    1,085
    ... or (to simplify)

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function calculate(meth)
    {
    var a=Number(document.cal.num1.value);
    var b=Number(document.cal.num2.value);
    switch (meth){
    case "add":
    var c=a+b;
    break;
    case "sub":
    var c=a-b;
    break;
    case "mul":
    var c=a*b;
    break;
    case "div":
    var c=a/b;
    break;
    }
    document.cal.res.value=c;
    	
    }
    
    </script>
    </head>
    
    <body>
    <form name="cal">
    Enter 1st num:<input type="text" name="num1" /> <br/>
    Enter 2nd num:<input type-="text" name="num2" /> <br/>
    Select operation:
    <select name="-please select-" onchange="calculate(this.value)">
    <option >-please select-</option>
    <option value="add">addition</option>
    <option value="sub">subtraction</option>
    <option value="mul">multiplication</option>
    <option value="div">division</option>
    </select>
    
    Result= <input type="text" name="res" />
    </form>
    </body>
    </html>

  5. #5
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    lol
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles