Marcin Doliwa
Marcin Doliwa

Reputation: 3659

Pressing enter on form input and changing default action (submit form)

I have simple form with:

http://www.bootply.com/KMfGxnrqxf

When I fill facebook event link input with for example: https://www.facebook.com/events/123 and click import details it all works fine - changes link to /events/new?fb_event_id=123 and makes new request.

Now I try to do the same by pressing enter key instead of mouse click. It changes href to the correct one, but doesn't request this new url.

Any idea why?

Upvotes: 2

Views: 630

Answers (2)

Artur Filipiak
Artur Filipiak

Reputation: 9157

The native anchor click behavior can be triggered using this:

$(element)[0].click();

It getting the first element of the elements array (DOM element instead of jQuery object).

Solution reference

Upvotes: 2

andi
andi

Reputation: 6522

When you trigger the click event using $('#import').click();, that only kicks off the JS event handlers. It does not trigger the natural event associated with that element, i.e. it will not actually redirect the page to the anchor's href. You need to add that redirect into your JS event handler using something like:

window.location.href = '/events/new?fb_event_id='+event_id;

Upvotes: 2

Related Questions