SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast mtran's Avatar
    Join Date
    Apr 2006
    San Jose, US
    0 Post(s)
    0 Thread(s)

    form validation and array Help!

    Hi, just start learning validation with jscript and need help with this code.

    I create this simple function.
    function add(){ 
      document.form.name2.disabled = false;
      document.form.title2.disabled = false;
      document.form.email2.disabled = false; }
    It works fine with my onclick. When I click on the checkbox, all of the mentioned fields become active.
    <input type="checkbox" name="add_attendee" onclick="add();" /> Add attendee #2
    I like to kind of generalize the function and turn it into e.g.:

    var add = new Array("name2","title2","email2");
    var question3 = new Array("q31","q32","q33");

    function enable(box){
    for (i=0;i<box.length;i++){[i].disabled = false;}}

    So that, later no matter what question I want to enable, I just replace "box" with another array like "add" or "question3" using onlclick ="enable(add)"; or onclick = enable(question3)".

    I tested it and it didn't work. Could anybody help? I used the same method for flash actionscript and everything works fine, but not in jscript.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    0 Post(s)
    0 Thread(s)
    First, the language is called javascript--no matter what MS calls it. Or, js for short.

    Could anybody help?
    Sure. box[i] is a string. If i=0 and the 'add' array is sent to your function, then box[i] is the string "name2". However, when you have a statement like this:


    none of those terms is a string. Each of the terms is a reference to an object. You don't really have to know what that means--it's enough to know that none of those terms are strings. Therefore, none of them can be replaced by a string such as box[i]. For instance, this doesn't work:
    document.form."name2".disabled = false;
    In the line:
    document.form.name2.disabled = false;
    name2 is actually an object that js creates to represent your html element--the element with the name attribute "name2". The name2 object has several properties you can access, like the .disabled property or the .type property or the .checked property. On the other hand, the string "name2" does not have a .disabled property or a .type property or a .checked property. After all, the string "name2" is not part of a form--it's just a series of characters that form a string. The string "name2" is an object in its own right, though, and as such, it has some properties too, like .length. So name2 is something entirely different than the string "name2".

    However, there is a different syntax that allows you to use strings to access elements of a form:
    document.forms["theFormName"].elements["theElementName"].disabled = false;
    Any of the strings in that statement can be replaced by the strings in an array, e.g.:
    document.forms["theFormName"].elements[box[i]].disabled = false;
    In js, whenever you have just a string, there is always away to get the object.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts