$("#myForm").attr("action") returns a form element instead of undefined for forms with no actions

11,646

Solution 1

It's a "bug" introduced by Netscape a long time ago (a boneheaded move, IMO), where form.action is a property because an element with that name is a child of the <form>. So no, it's not really a jQuery bug, but a JavaScript one, depending on your point of view...jQuery just doesn't have any additional checks for these cases.

To be safe, don't name your elements "action" or "submit", since it can mess with form.submit() as well.

Solution 2

Very odd indeed. I am going to quietly say - bug...

Form with an action attribute: http://jsfiddle.net/vMTYY/

Form without an action attribute: http://jsfiddle.net/ZWWTm/

Share:
11,646
KeatsKelleher
Author by

KeatsKelleher

Tech Lead for Buzzfeed's Optimization Squad and the POUND project on network diffusion, @KeatsKelleher on twitter

Updated on June 09, 2022

Comments

  • KeatsKelleher
    KeatsKelleher almost 2 years

    I have this basic example:

    <!doctype HTML>
    <html>
    <head>
        <script src="jquery-1.4.2.min.js" type="text/javascript"></script>
        <script>
            $(document).ready(function(){
                $("#showAction").click(function(){
                    alert($("#myForm").attr("action"));
                });
            });
        </script>
    </head>
    <body>
    <form id="myForm">
        <input type="text" name="action" value="myAction" />
    </form>
    <input type="button" value="click me" id="showAction" />
    </body>
    </html>
    

    When you click 'click me' you can see the tag

    $("#myForm").attr("action"); 
    

    Doesn't actually return an attribute of the element. It returns the child of the form with the name "action".

    Is this expected behavior? Is this a bug in jQuery?

  • Pekka
    Pekka over 13 years
    A nice reminder that IE isn't the only culprit for the kludgy early days of the Internet. +1
  • lonesomeday
    lonesomeday over 13 years
    +1 I suppose this is because, effectively, $('form').attr('action') is a wrapper for $('form')[0].action?
  • KeatsKelleher
    KeatsKelleher over 13 years
    hah...it's funny Wordpress should choose to require an 'action' parameter to be posted to it's admin-ajax.php script to process forms.
  • Nick Craver
    Nick Craver over 13 years
    @lonesomeday - it'll fall back to that, yes :)
  • lonesomeday
    lonesomeday over 13 years
    @Nick Presumably your advice goes for method, enctype, accept and accept-charset as well?
  • Nick Craver
    Nick Craver over 13 years
    @lonesomeday - if they're not defined on the <form>, yes, any property will fall back and give some unexpected results
  • Joe Johnson
    Joe Johnson over 11 years
    This is NOT a bug. If, however, you do not know the properties of a FORM element, you can easily introduce a bug (overriding the form's action attribute/property with an element named the same) into your non-well-formed HTML document. As already stated, just don't use reserved words as form element names.