help with document.formname.variable.value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott

    #1

    help with document.formname.variable.value

    I need to write a function that copies variables to fields. I've used an
    array and loop because it's neater than writing a similar sentence out
    10 times.

    var myString = new
    String("id,call ername,address, phone,phoneb,em ail,sqft,source ,action,status" );
    var myFields = myString.split( ',');

    //myValues is an array of values passed to the function
    var myValues =
    [id,callername,a ddress,phone,ph oneb,email,sqft ,source,action, status];

    for(var i=0;i<myFields. length;i++) {
    document.formna me.myFields[i].value=myValues[i];
    }

    Firefox returns 'document.edit. myFields' has no properties. IE doesnt
    like it either. I'm sure this is a simple question but Google has turned
    up nothing :(
  • RobG

    #2
    Re: help with document.formna me.variable.val ue

    Scott wrote:[color=blue]
    > I need to write a function that copies variables to fields. I've used an
    > array and loop because it's neater than writing a similar sentence out
    > 10 times.
    >
    > var myString = new
    > String("id,call ername,address, phone,phoneb,em ail,sqft,source ,action,status" );
    >
    > var myFields = myString.split( ',');
    >[/color]

    It seems a bit pointless to create a string purely for the sake
    of then creating an array. Why not just create an array?

    var myFields = [
    'id','callernam e','address','p hone',
    'phoneb','email ','sqft','sourc e',
    'action','statu s'
    ];

    [color=blue]
    > //myValues is an array of values passed to the function
    > var myValues =
    > [id,callername,a ddress,phone,ph oneb,email,sqft ,source,action, status];[/color]

    whatever was passed has now obliterated by re-initialising the
    variable. It isn't good to have an input element called 'id',
    it will be easily confused with the id attribute and may cause
    issues.

    You should have something like:


    function theFuntion() {
    ....

    And call it with (presuming the values are passed as strings):

    theFunction('an Id','aCallernam e','anAddress', 'aPhone',
    'aPhoneB','anEm ail','aSqft','t heSource',
    'theAction','th eStatus');
    ...

    now use the arguments collection to get at the values passed to
    the function. Or, you can pass an array:

    function theFuntion(myVa lues) {
    ....

    var myData = ['anId','aCaller name','anAddres s','aPhone',
    'aPhoneB','anEm ail','aSqft','t heSource',
    'theAction','th eStatus'];
    theFunction(myD ata);
    ...

    You should also pass a reference to the form to make life
    easier:

    function theFuntion(theF orm, myValues) {
    ....

    var myData = ['anId','aCaller name','anAddres s','aPhone',
    'aPhoneB','anEm ail','aSqft','t heSource',
    'theAction','th eStatus'];
    theFunction(thi s.form, myData);
    ...
    [color=blue]
    >
    > for(var i=0;i<myFields. length;i++) {
    > document.formna me.myFields[i].value=myValues[i];
    > }[/color]

    Try this:

    <script type="text/javascript">
    function addValues(f, myValues){
    var myFields = ['Id','callernam e','address','p hone',
    'phoneb','email ','sqft','sourc e','action','st atus'];
    for(var i=0, len=myFields.le ngth; i<len; i++) {
    f.elements[myFields[i]].value = myValues[i];
    }
    }
    </script>

    <form action="">
    <input name="Id" type="text" size="20">Id<br >
    <input name="callernam e" type="text" size="20">calle rname<br>
    <input name="address" type="text" size="20">addre ss<br>
    <input name="phone" type="text" size="20">phone <br>
    <input name="phoneb" type="text" size="20">phone b<br>
    <input name="email" type="text" size="20">email <br>
    <input name="sqft" type="text" size="20">sqft< br>
    <input name="source" type="text" size="20">sourc e<br>
    <input name="action" type="text" size="20">actio n<br>
    <input name="status" type="text" size="20">statu s<br>
    <input type="button" value="Add values" onclick="
    var theValues = ['anId','aCaller name','anAddres s',
    'aPhone','aPhon eB','anEmail',' aSqft',
    'theSource','th eAction','theSt atus'];
    addValues(this. form,theValues) ;
    "><br>
    <input type="reset">
    </form>




    --
    Rob

    Comment

    • RobB

      #3
      Re: help with document.formna me.variable.val ue

      Scott wrote:[color=blue]
      > I need to write a function that copies variables to fields. I've used[/color]
      an[color=blue]
      > array and loop because it's neater than writing a similar sentence[/color]
      out[color=blue]
      > 10 times.
      >
      > var myString = new
      >[/color]
      String("id,call ername,address, phone,phoneb,em ail,sqft,source ,action,status" );[color=blue]
      > var myFields = myString.split( ',');
      >
      > //myValues is an array of values passed to the function
      > var myValues =
      > [id,callername,a ddress,phone,ph oneb,email,sqft ,source,action, status];
      >
      > for(var i=0;i<myFields. length;i++) {
      > document.formna me.myFields[i].value=myValues[i];
      > }
      >
      > Firefox returns 'document.edit. myFields' has no properties. IE doesnt[/color]
      [color=blue]
      > like it either. I'm sure this is a simple question but Google has[/color]
      turned[color=blue]
      > up nothing :([/color]

      Just use a simple object---> { name: value , name: value....} to store
      the data. It's an analogue of the form structure you're populating;

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">
      <html>
      <head>
      <title>untitled </title>
      <script type="text/javascript">

      var elvals =
      {
      id: 'Mr. Id' ,
      callername: 'Mrs. Caller' ,
      address: '22 Belgravia' ,
      phone: '444-657-6797' ,
      phoneb: '555-769-0755' ,
      email: 'blah@feh.net' ,
      sqft: '246' ,
      source: 'the Sun' ,
      action: 'the Movement' ,
      status: 'shmatus'
      }

      window.onload = function()
      {
      var el, f = document.forms. edit;
      for (elname in elvals)
      if (el = f.elements[elname])
      el.value = elvals[elname];
      }

      </script>
      </head>
      <body>
      <form name="edit">
      <ul>
      <li><input type="text" name="id" value="">__id</li>
      <li><input type="text" name="callernam e" value="">__name </li>
      <li><input type="text" name="address" value="">__addr ess</li>
      <li><input type="text" name="phone" value="">__phon e</li>
      <li><input type="text" name="phoneb" value="">__phon e 2</li>
      <li><input type="text" name="email" value="">__emai l</li>
      <li><input type="text" name="sqft" value="">__sq. ft.</li>
      <li><input type="text" name="source" value="">__sour ce</li>
      <li><input type="text" name="action" value="">__acti on</li>
      <li><input type="text" name="status" value="">__stat us</li>
      </ul>
      </form>
      </body>
      </html>
      [color=blue][color=green]
      >> I'm sure this is a simple question but Google has turned[/color][/color]
      up nothing :(

      That's because it's in the FAQ...



      Comment

      Working...