SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jan 2002
    Posts
    245
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form help please

    Hi !

    I'm playing around with this bit of javascript which totals up the value of three fields into a text box.

    It seems to work fine but dreamweaver it telling me I should not have two values in the same text box.

    Heres the code. Can someone tell me if it is ok


    <form>
    <input type="text" name="chProduct1" value="0" onFocus="calcTotal(this.form)" value="Total">
    Product 1 - $5<br>
    <input type="text" name="chProduct2" value="0" onFocus="calcTotal(this.form)" value="Total">
    Product 2 - $50<br>
    <input type="text" name="chProduct3" value="0" onFocus="calcTotal(this.form)" value="Total">
    Product 3 - $500 <br>
    Total <input type="text" name="Total" value="" onChange="calcTotal(this.form)"><br>
    <br>
    </form>

    <script>
    function calcTotal(frm){
    frm.Total.value = 0;
    for(i=0;i<frm.length;i++){
    if(frm.elements[i].name.substr(0,2)=="ch"&&frm.elements[i]){
    frm.Total.value = parseInt(frm.Total.value) + parseInt(frm.elements[i].value);
    }
    }
    }
    </script>


    thanks

  2. #2
    SitePoint Member Prime1's Avatar
    Join Date
    Nov 2002
    Location
    Ceravolo
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up works good !

    I tried it just now,

    works great in my frontpage Express, I will try later in dreamweaver but the code seems fine. I changed the $ to aswell, and all okay!

    the code looks good.
    If it ain't broke, Don't Fix it!

    This is my job

  3. #3
    SitePoint Addict
    Join Date
    Jan 2002
    Posts
    245
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    You might find this useful!

    Thanks Prime,

    I carried on messing with the script and now have it working the way I want it to:

    Here is the script if anyone wants to use it.


    Qamar


    <form >
    <table border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b>1</b></font></td>
    <td><img src="pics/spacer.gif" width="10" height="11"> </td>
    <td>
    <input type="text" name="M_c1" size="10" value="0" onChange="calcTotal(this.form)">
    </td>
    </tr>
    <tr>
    <td><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b>2</b></font></td>
    <td><img src="pics/spacer.gif" width="10" height="11"> </td>
    <td>
    <input type="text" name="M_c2" size="10" value="0" onChange="calcTotal(this.form)">
    </td>
    </tr>
    <tr>
    <td><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b>3</b></font></td>
    <td><img src="pics/spacer.gif" width="10" height="11"> </td>
    <td>
    <input type="text" name="M_c3" size="10" value="0" onChange="calcTotal(this.form)">
    </td>
    </tr>
    </table>
    <font size="1" face="Verdana, Arial, Helvetica, sans-serif"><b> </b></font>
    <p><font size="1" face="Verdana, Arial, Helvetica, sans-serif"></font></p>
    <p>&nbsp; </p>
    <table border="0" cellspacing="0" cellpadding="3">
    <tr bgcolor="#CCCCCC">
    <td nowrap><font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCCC"><b><font color="#333333">Total
    amount</font></b></font></td>
    <td><font color="#CCCCCC"></font></td>
    <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif" color="#CCCCCC"><b>
    <input type="text" size="10" value="1" name="amount" onChange="calcTotal(this.form)">
    </b></font></td>
    </tr>
    </table>
    <p>&nbsp; </p>
    </form>
    <script language="">
    function calcTotal(frm){
    frm.amount.value = 0;
    for(i=0;i<frm.length;i++){
    if(frm.elements[i].name.substr(0,3)=="M_c"&&frm.elements[i]){
    frm.amount.value = parseInt(frm.amount.value) + parseInt(frm.elements[i].value);
    }
    }
    }

    </script>


Bookmarks

Posting Permissions

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