CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Adding elements

  1. #1
    Join Date
    May 1999
    Location
    Mid-West
    Posts
    50

    Adding elements

    I have a web page which is nicely generated through a database query. Basically it's a form that can be changed on any occasion. These forms have text boxes which need to be added up for a total box at the bottom of the page. There are two issues. First i would like to be able to add those elements on the client side (since i'm not actually storing the added total). Secondly the text boxes are named differently to relate to a key in the database for later updating and inserting. I need to be able to auto generate Javascript on the server side to add all the elements on a 'onchange' event. Below is an example of what i mean.

    function TOTAL_onchange() {
    var temp = 0.00;
    temp = Number(document.thisForm.TOTAL.value) +
    Number(document.thisForm.TOTAL.value);
    document.thisForm.TOTAL.value = temp;

    }
    the TOTAL variable will be replaced and repeated according to how many elements exist on the page. If you need any clarifiaction please let me know.
    Thanks


  2. #2
    Join Date
    May 1999
    Location
    Mid-West
    Posts
    50

    Re: Adding elements

    I figured it out. I simple make all the names of each text box as a number or ID. Then use the following piece of code.
    function TOTAL_onchange()
    {
    dml = document.thisForm;
    len = dml.elements.length;
    var i=0;
    var Total = 0.00;
    for( i=0 ; i<len ; i++)
    {
    if (isNumeric(document.thisForm.elements[i].name) )
    {
    Total = Total + Number(dml.elements[i].value)
    }
    }
    document.thisForm.TOTAL.value = Total;
    }

    function isNumeric(text)
    {
    var rc;
    text += ""; //convert to string
    rc = text.match(/^[0-9]*$/);
    if (rc == null)
    {return false;}
    else {return true;}
    }

    Thanks


  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Adding elements

    Matt, if you need any JavaScript help - here's my email
    [email protected]


    F. T. W.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured