Web Analytics Made Easy -
StatCounter using a variable in a document string - CodingForum

Announcement

Collapse
No announcement yet.

using a variable in a document string

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • using a variable in a document string

    How do i use a variable in a string like > form.myform.t_field.value

    i have 3 different fields with i woud like to add to
    but insted of making an "if" statment for each, i was thinking of doing somthing like this :

    function MyAction(user, number){
    t_field = "t_" + user
    var a= (form.myform.t_field.value)
    var b= eval(a + number)
    form.myform.t_field.value = b
    }

    But it dont seems to like that ??

    Annyone that van help
    Last edited by Jesper Mّller; Sep 23, 2006, 12:48 PM.
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #2
    Try this...

    Code:
    function MyAction(user, number){
    t_field = "t_" + user
    var a= form.myform[t_field].value
    var b= eval(a + number)
    form.myform[t_field].value = b
    }

    Comment


    • #3
      Thanks :-)
      "True knowledge exists in knowing that you know nothing."

      "Education is learning what you didn't even know you didn't know!"

      Comment


      • #4
        don't use eval(). It is usless in your case, and it is an evil method, it might do some harm and eat memory resources. Presuming that number is indeed a decimal number, not a string, you may simply use
        Code:
        function MyAction(user,number){
        var obj = form.myform['t_'+user];
        obj.value=Number(obj.value)+number;
        }
        KOR
        Offshore programming
        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

        Comment

        Working...
        X