SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    weird behaviour of Form Object's name property

    Hi

    I have a form that has a text field with a name "name". I tried validating this form and in one of functions for debuging purposes added this line of code.

    alert(form.name); // form is a reference to form element

    In alert box I would get [object] which is definately not a name of a form as expected. As it appeared that form.name refered to the form's name text field and unless I changed it to firstname form.name would not return the name of the form.

    Strange behaviour!

    Have you ever encountered this? Probably I am doing something wrong. Please let me know.

    Thanx

    IL

  2. #2
    Maniacally depressed robot poncho's Avatar
    Join Date
    Dec 2004
    Location
    Belfast, N.Ireland
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This behaviour comes from the browser's document object model, ie. you can access the value of a field called 'email' by doing this:

    PHP Code:
    alert(form.email.value); 
    You could get the 'name' attribute of the form using the 'getAttribute()' method like so.

    PHP Code:
    var formName document.forms[0].getAttribute('name');
    alert(formName); 
    Cheers;
    Poncho
    Perfecting the art of breaking stuff.
    Check 'em: CakePHP | TextMate

  3. #3
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Huge thanx!

    This will save me some headaches.

    Regards

    IL

  4. #4
    Maniacally depressed robot poncho's Avatar
    Join Date
    Dec 2004
    Location
    Belfast, N.Ireland
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem,

    Here is some recommended reading for all your javascript needs.

    Cheers;
    Poncho
    Perfecting the art of breaking stuff.
    Check 'em: CakePHP | TextMate


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
  •