forms - jQuery - Multiple Triggers for the Same Function -
i'm developing jquery, i'm beginner it's little bit bloated , not elegant. basically, want cookie value nullified when either 1 of 2 forms submitted or submit buttons clicked. don't know how consolidate conditions of function have write out function once, best can this:
$('form#searchform-basic').submit(function() { $.cookie('tab_cookie', null); }); $('form#searchform-basic a.search').click(function() { $.cookie('tab_cookie', null); }); $('form#searchform-advanced').submit(function() { $.cookie('tab_cookie', null); }); $('form#searchform-advanced input#search-button-submit').click(function() { $.cookie('tab_cookie', null); });
there's got better way, right?
and before asks, yes, think you'd need know when forms being submitted. leaving out click events means function doesn't run when submit buttons clicked. don't know why that, is.
give function name, so:
function clearcookie() { $.cookie('tab_cookie', null); }
that way, can call multiple events needed.
$('form#searchform-basic').submit(clearcookie); $('form#searchform-basic a.search').click(clearcookie); $('form#searchform-advanced').submit(clearcookie); $('form#searchform-advanced input#search-button-submit').click(clearcookie);
voila!
to take 1 step further, can combine selectors calling same event (submit or click), so:
$('form#searchform-basic, form#searchform-advanced').submit(clearcookie); $('form#searchform-basic a.search, form#searchform-advanced input#search-button-submit').click(clearcookie);
i leave off form tag , use ids, me.
$('#searchform-basic, #searchform-advanced').submit(clearcookie); $('#searchform-basic a.search, #searchform-advanced input#search-button-submit').click(clearcookie);
Comments
Post a Comment