Accessing arrays via DOM

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CathInfo
    New Member
    • Feb 2008
    • 2

    #1

    Accessing arrays via DOM

    I want to use an array for my fieldnames (field[1], field[2], etc.) instead of names like "first_name ", "last_name" , etc.

    It works from an HTML standpoint, but I can't seem to access the contents of those text input fields from Javascript. The code below works 100% if I change "field[3]" to any normal variable name like "last_name" .

    [CODE=javascript] if (cust_last_name .length < 1) {
    eval('document. forms.info.fiel d[3].style.backgrou ndColor = "#FFCCCC";' );
    eval('document. forms.info.fiel d[3].focus();');
    alert("Please fill in the Last Name field.");
    return false;
    }
    [/CODE]
    How can I do what I'm trying to do?

    Thanks,

    Matthew
    Last edited by acoder; Feb 28 '08, 08:20 AM. Reason: Added code tags
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Originally posted by CathInfo
    I want to use an array for my fieldnames (field[1], field[2], etc.) instead of names like "first_name ", "last_name" , etc.

    It works from an HTML standpoint, but I can't seem to access the contents of those text input fields from Javascript. The code below works 100% if I change "field[3]" to any normal variable name like "last_name" .

    if (cust_last_name .length < 1) {
    eval('document. forms.info.fiel d[3].style.backgrou ndColor = "#FFCCCC";' );
    eval('document. forms.info.fiel d[3].focus();');
    alert("Please fill in the Last Name field.");
    return false;
    }

    How can I do what I'm trying to do?

    Thanks,

    Matthew
    I never tried anything like that, but if you really want to use arrays,
    then you may use the standard DOM array, ie. elements[ ]

    You do not need to provide elements[ ] as name to the form items.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      No need for eval either:
      [CODE=javascript] if (cust_last_name .length < 1) {
      document.forms["info"].elements["field[3]"].style.backgrou ndColor = "#FFCCCC";
      document.forms["info"].elements["field[3]"].focus();
      alert("Please fill in the Last Name field.");
      return false;
      }
      [/CODE]

      Comment

      Working...