views:

80

answers:

6

I was having this issue with jQuery not being able to find a <form> element in my HTML for the past day and a half. I ran my HTML through validators multiple times, validated my JS and CSS, and even removed unnecessary JS and CSS files, which is now reduced to this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;

<html>
<head>
    <title>Test</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.0r4/build/reset/reset-min.css" />
</head>

<body>
    <form>
        <input type="textbox" name="action" value="view"></input>
        <button>Test</button>
    </form>
</body>
</html>

After reducing my HTML to bare bones, I checked if jQuery could simply grab the <form> element using $('form'). Well, jQuery returned nothing. It completely crapped out. It returned nothing, not even a jQuery object. So again I continued on with my insanity, validating the HTML, etc. etc., and still nothing!

I rewrote the HTML a second time, but not exactly the same way, and it worked! So I diff'd the two files and started tweaking the original file to incrementally become identical to the new file I had just written. Well it turns out that the value of the name attribute was the culprit that was screwing everything up! Simply changing the the value to anything other than action allowed jQuery to finally pick up the <form> element! Really?! !@#%$!@#!!!

So my questions are: Is action a keyword in jQuery and how does the value of an attribute screw everything up?

If it is a keyword and you don't feel like explaining why, could you please point me to the documentation on this so I can smack myself in the head after reading it. It still seems rather odd that the value of an attribute would screw things up, but that's probably because I'm ignorant of jQuery's implementation. =)

A: 

ACTION is a required attribute of the FORM tag.

EDIT

Your given example is not valid HTML. Try:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
<head>
 <title>Test</title>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
 <script type="text/javascript">
 $(document).ready(function() {
  $('form').css({"border":"1px solid red"});
 });
 </script>
</head>
<body>
 <form action="">
 <fieldset><legend>Test</legend>
  <input type="text" name="action" value="view"/>
  <button>Test</button>
  </fieldset>
 </form>
</body>
</html>
graphicdivine
Sorry about that. I knew it wasn't completely valid after I stripped my HTML down to just bare bones. I was just trying to see if jQuery could select the form element from something so minimal. So you're right, it isn't valid HTML, but should that stop jQuery from being able to select the form element?It shouldn't. My actual problem was with the way I was testing. I was using the Firebug Console in FF3.5, but not checking if the document was ready first. But even adding the `action` attribute to the form element still does not work with my original testing method.
John
+1  A: 

Is action a keyword in jQuery and how does the value of an attribute screw everything up?

No, it is not. Your problem lies somewhere else. Maybe you're trying to get the form directly in the HTML head instead of waiting for the page to finish loading.

E.g. you did

<script>
    alert($('form').length); // May return 0 depending on the browser used.
</script>
...
<form>
    ...
</form>

instead of

<script>
    $(document).ready(function() {
        alert($('form').length); // Always returns 1.
    });
</script>
...
<form>
    ...
</form>
BalusC
+1  A: 

I just copy/pasted your example code and added

<script type="text/javascript">
    jQuery('form').html('It works fine');
</script>

… after the form element. The content of the form was replaced with "It works fine", so no, jQuery doesn't have that problem.

David Dorward
+1  A: 

If I'm remembering correctly, there can be issues in Internet Explorer (6) if you use name="action". Somehow action is a reserved word. Don't remember the consequences thought, but stuff didn't work. So if you're on MSIE, try another browser too.

Samuel Sjöberg
+1  A: 

Can't reproduce your problem

Check this site http://jsbin.com/ixixu3

The alert always shows 1 for all the different selectors (tested in Opera 10.10, IE6, FF 3.5.6)

jitter
A: 

Thanks for all the information everyone. I should have mentioned that I was using FF3.5 and testing using the Firebug Console. I would load the page and then execute $('form') in Firebug. It would return nothing every time when I had the name attribute set to action. After changing it to something else, executing that same line of code in the Firebug Console would work.

After reading everyone's posts, I edited my HTML by adding a <script> element to the <head> section:

<head>
    <title>Test</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.0r4/build/reset/reset-min.css" />
    <script type="text/javascript">
        $(function(){
           alert($('form').length); 
        });
    </script>
</head>

This worked completely fine. So I guess my error was in my testing methods. Using $('form').length in the Firebug Console yields nothing. What I should have used this line instead: $(function(){$('form');});$(function(){$('form');})` I didn't think I would need to check if the document was ready first before trying to select the form element using the Firebug Console; I was wrong.

MORAL OF THE STORY: Even in Firebug, you still need to check if the document is ready before selecting the element.

What still bothers me though is that when the name attribute is not action, then my original testing method works with the Firebug Console. So perhaps the real question should have been: What's Firebug's beef with the value action for the name attribute on an <input> element? Ah well, we'll save that for another time.

Thanks everyone for the help! All your posts lead me to this final realization, it's too bad I can't give everyone a checkmark but I will give everyone a vote!

John