SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    can I pass the document object as a parameter?

    I want to have a validation function in a .js file that can be called from many different pages. I was trying to design the function so that it would accept an array of element names to be validated. My plan was to loop through each name in the array and check that element's value to make sure it has been completed. If it hasn't been completed (if it is blank), then I would alert the user. So my code would be something like:

    function validate(doc, a){
    var s;
    for(var i = 0; i < a.length; i++){
    s = doc.getElementByID(a[i]).value;
    if ( s.replace(/ /g, "") == ""){
    //generate some sort of warning, etc...
    }
    }
    }

    The doc parameter in the example above would be the document object for the calling page, like this:
    function do_validate(){
    var a = new Array("FirstName", "LastName", "EMail");
    validate(document, a);
    }

    But, when the validate code runs in the .js file, I get an "Object doesn't support this property or method" error on the line "s = doc.getElementByID(..."

    I tried renaming the parameter to 'document' instead of 'doc' but, that did not help.

    It seems like I might need explicitly "cast" the doc argument as a document, or something like that. But, that isn't done in javascript is it? I am new to javascript so, I am at a loss here.

    I apologize for the long post.

    Thanks for any help you can provide.
    rogdawg

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Be careful with the case of your methods.

    It should be getElementById() with a small d.

  3. #3
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh, for crying out loud!

    I could have banged my head against that for hours without spotting it.

    Thanks so much!


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
  •