SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict greg76's Avatar
    Join Date
    Aug 2004
    Location
    Poland
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    dynamic file name inside a function

    EDITED: of course, NOT a file name! I meant field's name, not file.

    Hi,

    I cannot make it work, don't even know what do I do wrong - learning JS comes to me in great pain

    I have a simple function:
    Code javascript:
     
    function myFunction(field){
        var myfield = document.myForm.field;
     
    .
    .
    .
    }

    which I want to apply to different fields in my form - all textarea fields, with different names.
    How can I make a Var inside the function, that dynamically assigns the proper name?

    Thanks in advance!
    Greg
    Last edited by greg76; Apr 10, 2008 at 18:06. Reason: pressed damn Enter by accident

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    var myfield = document.myForm[field]
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    May 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by greg76 View Post
    EDITED: of course, NOT a file name! I meant field's name, not file.

    Hi,

    I cannot make it work, don't even know what do I do wrong - learning JS comes to me in great pain

    I have a simple function:
    Code javascript:
     
    function myFunction(field){
        var myfield = document.myForm.field;
     
    .
    .
    .
    }

    which I want to apply to different fields in my form - all textarea fields, with different names.
    How can I make a Var inside the function, that dynamically assigns the proper name?

    Thanks in advance!
    Greg
    Hi Greg,

    Change to the code to this:

    HTML Code:
    var myField = document.myForm[field].value;
    Rey...

  4. #4
    SitePoint Addict greg76's Avatar
    Join Date
    Aug 2004
    Location
    Poland
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, guys

    gonna try it out in the morning,
    Thanks!

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Hey Rey, that will assign the value from the field to the variable, but it will be no good he wants to change that value in the field

    By assigning the element itself to the variable you can then easily obtain access to the value when desired, and also other useful properties as well.

    So in summary

    Code javascript:
    var myfield = document.myForm[field];
    var fieldValue = myfield.value;
    myfield.value = someFunction(fieldValue);
    Last edited by paul_wilkins; Apr 10, 2008 at 22:29.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Addict greg76's Avatar
    Join Date
    Aug 2004
    Location
    Poland
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Code javascript:
    var myfield = document.myForm[field];
    Worked like a charm
    Thanks guys!

    Appreciate your time,
    cheers,

    Greg

  7. #7
    SitePoint Zealot Ken Sharpe's Avatar
    Join Date
    Apr 2008
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way, firebug is a totally invaluable tool for those who write (or want to learn) javascript. It has a fully functional debugger that allows you to set breakpoints, add watches, etc -- it's really nice, if you haven't tried it already!


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
  •