#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    toronto, ontario, canada
    Posts
    10
    Rep Power
    0

    javascript functions


    I have a form which is organized with a table, each row of the table has several form elements (at least 3 text fields each)

    <form>
    <tr>
    <input type=text name="q1">
    <input type=text name="q1_value">
    <input type=text name="q1_total">
    </tr>
    <tr>
    <input type=text name="q2">
    <input type=text name="q2_value">
    <select option ... name="q2_dazy">
    <input type=text name="q2_total">
    </tr>

    ...
    <tr>
    <input type=text name="q30">
    <input type=text name="q30_value">
    <select size=1 ... name="q30_dazy">
    <select size=1 ... name="q30_lazy">
    <input type=text name="q30_total">

    </tr>...
    <form>

    Now I'm writing a javascript function, that'll fill in *_total based on *_value and a constant. i.e. q3_total = q3_value * 50;

    My problem is that my way is really long, I have a lot of if statements to do it. Is there a better way?

    my way:
    function(name)
    if (name == q1)
    q1_total = q1_value * 50;
    ...
    if (name == q30)
    q30_total = q30_value * 50;






    9
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    Belarus
    Posts
    30
    Rep Power
    14
    hi, underwarez

    I think you should do it like this

    function getTotal(name)
    {
    eval("document.mainForm." + name + "_total.value = document.mainForm." + name + "_value.value * 50");
    }


    and html code for proper working

    <form name="mainForm">
    <tr>
    <input type=text name="q1">
    <input type=text name="q1_value">
    <input type ....

IMN logo majestic logo threadwatch logo seochat tools logo