SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict kivison's Avatar
    Join Date
    Dec 2004
    Location
    Whitley Bay, Tyne & Wear, UK
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calculate multiple values

    Hi Gurus.

    I am new to the whole javascript business. I am designing a page which would be enhanced by the whole javascript experience.

    Here goes
    I have a table which I have made work where I put some values in and the script works out the result on an onChange event.

    My question is that I want to make the table rows populate from a database so each of the javascript values will have to be unique for the value to be worked out on a specific row calculation.

    The code as it is works out the single value but how would i go about using the mechanics of the code to apply to multiple lines in the table.

    Code:
    Heres the page i have designed. 
    
    It has no database content yet but i hope you get the idea.
    
    <Script Language="JavaScript">
    
    function Calc(){
    var a,b,result;
    var v1,v2;
    a=frm1.txt1.value
    b=frm1.txt2.value
    b=frm1.txt2.value
    
    
    v1=parseInt(a);
    v2=parseInt(b);
    
    
    result=v1*v2;
    frm1.txt4.value=result;
    
    }
    
    </Script>
    </head>
    <body>
    <form name="frm1">
     <p>&nbsp;</p>
     <table width="500" border="1" cellspacing="0" cellpadding="0">
       <tr>
         <td>Val 1 </td>
         <td>Operator</td>
         <td>val2</td>
         <td>result</td>
       </tr>
       <tr>
         <td><input type="text" name="txt1" size="20" onChange="Calc()"></td>
         <td>Multiply X</td>
         <td><input type="text" name="txt2" size="20" onChange="Calc()"></td>
         <td><input type="text" name="txt4" size="20"></td>
       </tr>
     </table>
     <p>&nbsp;</p>
     <p>
     </form>
    Hope you can help


    King Regards


    Keith

  2. #2
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you mean that you need a function that can be called to different object without mixing up with each other, you should try to make a function class, and define some parameters in your calc() function to allow different object id to be passed in.

  3. #3
    SitePoint Addict kivison's Avatar
    Join Date
    Dec 2004
    Location
    Whitley Bay, Tyne & Wear, UK
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for getting back to me SimplyFu

    OH this looks like a challenge. Ive only just started with Javascript.

    Are there any good tutorials you can recommended? or is it the usual go forth and Google??

    Thanks
    Keith


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
  •