We help IT Professionals succeed at work.

Please fix this stupid jquery form submit

Medium Priority
435 Views
Last Modified: 2012-03-14
The form doesn't submit.
The action does trigger when pressing enter on the keyboard.

I know which line of code needs to change, '$(this).closest("form").submit();'

Just unsure of what it needs to change to.

It needs to be dynamic, so that it works on any form.
So anytime someone clicks enter in an input field, it will submit that input's parent form.

TIA

$(document).ready(function(){
	$('input').keydown(function(event){
		if(event.which==13){
			$(this).closest("form").submit();
		}
	});
})

Open in new window

Comment
Watch Question

Commented:
This is what I use...

 if (event.keyCode == '13') { 

            event.preventDefault(); //avoids default action
            $(this).parent().find('input[type=submit]').trigger('click');       
 }

Open in new window

Author

Commented:
I don't typically have submit inputs, i use query/javascript to submit the forms.
There are a TON of forms through out this site, so it isn't worth while to go back and change.

Unfortunately, I didn't write the site, just updating it ... Been an absolute nightmare.
Commented:
hmm, possibly this then.

   $(this).parents('form').submit();

Open in new window


If this doesn't work I'm not sure without seeing more of the big picture.

Author

Commented:
I thought I had tried that,  realized I had tried $(this).parent('form').submit(); with .parents() as the singular form - probably why it didn't work.

Thanks for the help !! worked perfectly.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.