SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variable after document.form.

    Hi all,

    this has got to be SO simple but i just cannot get it.

    this doesnt work and it should!! should it not?

    var a = "total";
    var b = 1;
    var c = a.b;
    var v = 'total'+b;
    var d = b.toString();
    document.form.X.value = new_total;

    Where X is the variable name. If i type total where x is....it works fine. if i use the variable a which is equal to 'total' anyway...it doesnt work!!!
    Basically ive tried replacing x with all letters and changing the name of the field im updating with 'total' and 'total1' etc.

    The only way i can get it to update the text field, is to manually type in the field name im changing. i just want to refer to a variable.

    ive looked around but seen too many complex examples using getElement and things like that.

    Any help would be a lifesaver!

  2. #2
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there,

    If you are trying to use a variable in your reference, use the [] eg. document.form[X].value = new_total;

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok now ive got this, but it still wont work

    function update()
    {
    var new_total = 100;
    var t = "total";
    document.form[t].value = new_total;
    }

    <form name="form">
    <table>
    <tr>
    <td><input type="text" onkeyup="update()" /></td>
    <td><input type="text" name="total" /></td>
    </tr>
    </table>
    </form>

  4. #4
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you forgetting to put proper script tags for your JavaScript function or testing in an older browser? Works okay for me.

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm just copied as my example above into a clean html file and like you say, it works fine!!

    must be something to do with the file im trying to put it into.

    Well at least im on the right track now! so thanks very much for that
    just got to find the part thats wrong in the main file.

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    got it!

  7. #7
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


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
  •