By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,650 Members | 753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,650 IT Pros & Developers. It's quick & easy.

jQuery .on() api and bypassing it

Frinavale
Expert Mod 5K+
P: 9,731
I am still a novice when it comes to using jQuery and it has been a while since I've developed in the web.

I am taking advantage of something and wanted to make sure that what I am doing is correct.

I using the jQuery .on() api attach an event handler function for form.submit events within the document.


What I have discovered is that the the event handler is not executed if I directly access the form to submit it; however, if I use jQuery to create it's form object, the event handler is executed.

For example if I use:
Expand|Select|Wrap|Line Numbers
  1. <select id="SelectedMasterID" onchange="this.form.submit();" name="SelectedMasterID"> 
  2. <!-- options -->
  3. </select>
The above code will bypass the event handler method.

However, if I use the following it will not be bypassed:
Expand|Select|Wrap|Line Numbers
  1. <select id="SelectedMasterID" onchange="$(this).parents('form').submit();" name="SelectedMasterID"> 
  2. <!-- options -->
  3. </select>
This is actually beneficial to my situation because in some cases I want to bypass the method attached to the submit event.

According to the .on() api documentation:
The .on() method attaches event handlers to the currently selected set of elements in the jQuery object.
So, am I correct in thinking that this.form.submit() will always bypass the event handler?

Thanks,

-Frinny
Oct 23 '13 #1

✓ answered by Dormilich

So, am I correct in thinking that this.form.submit() will always bypass the event handler?
thatís like with apples and pears. the submit() in this.form.submit() is different from the submit() in $('form').submit() (which is actually a shortcut for trigger('submit')).

further details may depend on how exactly you defined the handler.

Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
So, am I correct in thinking that this.form.submit() will always bypass the event handler?
thatís like with apples and pears. the submit() in this.form.submit() is different from the submit() in $('form').submit() (which is actually a shortcut for trigger('submit')).

further details may depend on how exactly you defined the handler.
Oct 23 '13 #2

Dormilich
Expert Mod 5K+
P: 8,639
although, if you are using jQuery, why are you using event attributes? wouldn’t it be easier to use
Expand|Select|Wrap|Line Numbers
  1. $('#SelectedMasterID').on('change', function() {
  2.     this.form.submit();
  3.     // or
  4.     $(this.form).submit();
  5.     // or
  6.     if (validate(this)) {
  7.         this.form.submit();
  8.     }
  9. });
  10.  
?
Oct 23 '13 #3

Frinavale
Expert Mod 5K+
P: 9,731
I wanted to handle every item that submits the form, not just that one <select>.

I ended up changing my design so that ajax calls are only made in particular cases.
Oct 24 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.