www.webdeveloper.com
Results 1 to 2 of 2

Thread: HELP! adding selected drop down values together

  1. #1
    Join Date
    Dec 2009
    Posts
    6

    Question HELP! adding selected drop down values together

    ok here the problem i want to add to values from drop-down menus i got it to work but its is adding as a string not a number

    for example i select from the first box the number 2 then i select form the second box the number 3 when i click the button to alert the answer it returns a value of 23 when it should have added the numbers and returned a value of 5

    heres the code----- and the link to this live
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head></head>
    <body>
    <script type="text/javascript">
    function OnChange2()
    {
    var x = document.getElementById('colorsselect').value
    var y = document.getElementById('colorsselect2').value
    var var3 = y+x;
    alert(var3);
    }
    </script>
    
    <script type="text/javascript">
    function bam()
    {
    var bed = document.getElementById('bed').selectedIndex;
    var bedval = document.getElementById('bed')[bed].value;
    var xxl = document.getElementById('xxl').selectedIndex;
    var xxlval = document.getElementById('xxl')[xxl].value;
    var total = bedval+xxlval;
    alert(total);
    }
    </script>
    <form action="">
    <select name='colors1' id='bed'>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    </select>
    
    <select name='colors2' id='xxl'>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">2</option>
    <option value="4">4</option>
    </select>
    </form>
    
    <button type="button" value="My button value" onclick="bam();">button</button>  
    
    </body>
    </html>
    

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,678
    Code:
    var var3 = y*1+x*1;// auto type the strings to numbers
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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