Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    208
    Thanks
    52
    Thanked 1 Time in 1 Post

    xhtml doctype and form name attribute in javascript?

    Hi, I have been attempting to transition to use of xhtml strict doctype and
    my text editor, BBEdit (on Mac) tells me, when I ask it to check syntax,
    that the attribute 'name' is not allowed in form object, as in any other
    form element that I tried to use it in. This begs the question, how do
    I script forms with javascript in the context of this doctype? None of
    the javascript texts I have address this issue (O'Reilly Rhino book and
    others).

    I could figure it out, but it appears to be a complicated process, just
    doing getElementById() and sorting it out.
    Does anyone have a reference to material that deals with this issue?
    Or do I just abandon xhtml strict doctypes?

    Thanks for time and attention
    JK

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    208
    Thanks
    52
    Thanked 1 Time in 1 Post

    further consideration

    Well, I dropped xhtml strict doctype and am using transitional,
    so the document validates when using name attribute in form tag
    and elsewhere.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    It's kind of a weird change, but indeed the proper thing to do is stop using names for <form>s and use ids instead.

    For people who were used to coding like this:
    Code:
    function validate( )
    {
        if ( document.form1.name.value == "" )....
        if ( document.form1.address.value == "" ) ...
    }
    then, yeah, it's ugly. You don't want to have to code
    Code:
    function validate( )
    {
        if ( document.getElementById("form1").name.value == "" )....
        if ( document.getElementById("form1").address.value == "" ) ...
    }
    But that always was a dumb way to code such things.

    I have always used something like:
    Code:
    function validate( )
    {
        var form = document.form1;
        if ( form.name.value == "" )....
        if ( form.address.value == "" ) ...
    }
    so for me it is trivial to switch over to
    Code:
        var form = document.getElementById("form1");
    Even more, whenever possible I don't give *EITHER* a name or id to my forms.

    After all, if you code:
    Code:
    function validate( form ) ...
    And then invoke it via
    Code:
        <form ... onsubmit="validate(this);">
    or maybe
    Code:
         <select name="whatever" onchange="if ( validate(this.form) ) this.form.submit();" >
    and so on, there's really no need for either name or id.

    Finally, if you only have one <form> on your page, which most of us do, you can always use
    Code:
        var form = document.forms[0];
    So I, for one, don't see that using forms without names is any change at all.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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