Recently I experience the following problem, when I call this code it triggers an error in FireBug:
With “human” words: I trired to find a input with a spefic name, then find the form wrapping this input and try to submit it. Strangely, but it trows an error in jQuery lib.
The form has the following structure:
Probably some of you will notice that this is search form from WordPress.
I searched in internet and found that there are similar issues in the jQuery bug tracker, but they are closed with “wontfix” message explaining that this is a Dom problem.
Here is the working example of the problem: click. Click on the submit button in the first form and it will do what is shold do, but button “not working” will trigger the error.
Ok, now the solution – find the button submit and trigger a click. Click on the button “working” in the first form in the example and you will see that it will submit properly the value.
Here is the solution:
//or if you know the position of the submit button
$('input[name="s"]').next().trigger('click');
The second form doesn’t have input with name or id “submit” and the normal submit() function is triggered correctly.
That’s it. Enjoy!
Ouch.
That’s a common gotcha, indeed. I have a better solution for you – just rename the submit button. Actually I got into the habit of naming my submits “commit” a few years ago and I’m pretty happy with it. Rails also does it.
(I’ve also blogged about this a few years ago, in Bulgarian, if you’re curious: http://skanev.com/2007/09/28/button-name-commit/)
You are right. In my day-to-day work I didn’t use even names for these butons, but this particular case I need to make it work for all cases. The reason is that I did this for my WordPress plugin Autocompleter and I cannot guarantee what theme is used and if this button is renamed. That’s why this solution should cover most of the cases.
You could also have changed “name” attribute of the Submit button.
E.g.
You could also have changed “name” attribute of the Submit button.
E.g.
<input type=”submit” class=”submit” name=”submit” id=”searchsubmit” value=”Search” />