dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: Simple problem, but can't figure it out

  1. #1
    Join Date
    Jul 2011
    Posts
    2

    Simple problem, but can't figure it out

    Working on a script that will add together dollar values in user selected drop downs and display on the page. I get the script to work if the total is to be displayed in a text box, but I want to do a document.write() to put it as a piece of text. I know the answer is simple, but I just don't have the experience to figure it out. I think it may have something to do with local/global scope of the variable. Searched everywhere for the solution.

    Below is the code, thanks to anyone who can help!

    -----------------------------------------------------------------

    <script language="JavaScript">


    function addall()
    {
    writetotal = document.summing.writetotal.value = parseFloat(document.summing.a.value)
    + parseFloat(document.summing.b.value)
    + parseFloat(document.summing.c.value)
    + parseFloat(document.summing.d.value)

    }


    </script>
    </head>

    <body>
    <form name="summing">


    <td width="502" valign="top">
    <select name="a" onChange="addall()">
    <option value="0.00">0</option>
    <option value="2.95">1</option>
    <option value="3.95">2</option>
    <option value="4.95">3</option>
    <option value="5.95">4</option>
    </select><br>
    <select name="b" onChange="addall()">
    <option value="0.00">0</option>
    <option value="2.95">1</option>
    <option value="3.95">2</option>
    <option value="4.95">3</option>
    <option value="5.95">4</option>
    </select><br>
    <select name="c" onChange="addall()">
    <option value="0.00">0</option>
    <option value="2.95">1</option>
    <option value="3.95">2</option>
    <option value="4.95">3</option>
    <option value="5.95">4</option>
    </select><br>
    <select name="d" onChange="addall()">
    <option value="0.00">0</option>
    <option value="2.95">1</option>
    <option value="3.95">2</option>
    <option value="4.95">3</option>
    <option value="5.95">4</option>
    </select><br><br>


    <input type="text" name="writetotal" size="5" maxlength="5">
    <input type="submit" name="Submit" value=""><br><br>

    <script language="JavaScript">
    <!-- hide from older browser

    document.write("Total: " + writetotal.toFixed(2)) //-->
    </script>




    </form>

  2. #2
    Join Date
    Jan 2010
    Location
    Winnipeg, Manitoba, Canada
    Posts
    95
    I changed it to a div if that's okay.
    HTML Code:
    <html><head><script language="JavaScript">
    
    
    function addall()
    {
    var writetotal=parseFloat(document.summing.a.value) 
    + parseFloat(document.summing.b.value)
    + parseFloat(document.summing.c.value)
    + parseFloat(document.summing.d.value);
    document.getElementById('wrtot').innerHTML=writetotal;
    
    }
    
    
    </script>
    </head>
    
    <body>
    <form name="summing">
    
    
    <td width="502" valign="top"> 
    <select name="a" onChange="addall()">
    <option value="0.00">0</option>
    <option value="2.95">1</option>
    <option value="3.95">2</option>
    <option value="4.95">3</option>
    <option value="5.95">4</option>
    </select><br>
    <select name="b" onChange="addall()">
    <option value="0.00">0</option>
    <option value="2.95">1</option>
    <option value="3.95">2</option>
    <option value="4.95">3</option>
    <option value="5.95">4</option>
    </select><br>
    <select name="c" onChange="addall()">
    <option value="0.00">0</option>
    <option value="2.95">1</option>
    <option value="3.95">2</option>
    <option value="4.95">3</option>
    <option value="5.95">4</option>
    </select><br>
    <select name="d" onChange="addall()">
    <option value="0.00">0</option>
    <option value="2.95">1</option>
    <option value="3.95">2</option>
    <option value="4.95">3</option>
    <option value="5.95">4</option>
    </select><br><br>
    
    
    <input type="text" name="writetotal" size="5" maxlength="5">
    <input type="submit" name="Submit" value=""><br><br>
    
    <div id="wrtot"></div>
    
    
    
    
    </form></body></html>

  3. #3
    Join Date
    Jul 2011
    Posts
    2

    It worked perfectly!

    Thanks! I would have never have gotten it. Your approach and solution was much better.

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