Instead of something like this
$('#submitbutton').click(function(e) {
e.preventDefault(); //to avoid the submit button to reload the page and go page to page one.
/* computing dimensions */
});
I'd like to put my function somewhere else, like this :
$('#submitbutton').click(computeUserDimensions);
function computeUserDimensions(){
/* computing dimensions */
}
But then I don't where to put the preventDefault to provide the click (which is on a submit button) to go to another page.
Can you help me figure this out?
Thanks
asked Apr 5, 2013 at 9:49
Louis
2,62013 gold badges68 silver badges124 bronze badges
-
put to bottom of the event the preventDefaultArpad Bajzath– Arpad Bajzath2013年04月05日 09:52:00 +00:00Commented Apr 5, 2013 at 9:52
3 Answers 3
Add parameter event to your function and it will be passed by jQuery implicitly.
$('#submitbutton').click(computeUserDimensions);
function computeUserDimensions(event){
/* computing dimensions */
event.preventDefault();
}
Sign up to request clarification or add additional context in comments.
5 Comments
Louis
Thanks, this works (but with
event̀ instead of e`). Thanks!Louis
What is the difference between writing
$('#submitbutton').click(computeUserDimensions); function computeUserDimensions(event){ ...} and $('#submitbutton').click(function() { computeUserDimensions(); }); function computeUserDimensions(event){ ...}Adil
In first you are passing reference of defined function and in second you are passing function (anonymous).
Louis
ok. Problem in this solution is if I want to reuse the function with another event, and don't want to preventDefault on this other function. The solution from Kami seems to handle this.
Adil
That method is already using two functions one anonymous and other named so you can make two named functions again and put the common code in one function and call from other.
You can change the function call to be an anonymous function that handles the click and calls the sub function.
$('#submitbutton').click(function(e) {e.preventDefault(); computeUserDimensions();});
function computeUserDimensions(){
/* computing dimensions */
}
answered Apr 5, 2013 at 9:51
Kami
19.5k4 gold badges53 silver badges65 bronze badges
2 Comments
Louis
what if I need to pass a variable (array) to computeUserDimensions in :
$('#submitbutton').click(function(e) {e.preventDefault(); computeUserDimensions();}); ? Should I write : $('#submitbutton').click(function(e) {e.preventDefault(); computeUserDimensions(prodata);}); Kami
@user130482 yes (provided
prodata is correctly populated array).function computeUserDimensions(e){
var evt = e ? e:window.event;
if (evt.preventDefault) evt.preventDefault();
/* computing dimensions */
}
answered Apr 5, 2013 at 9:52
palaѕн
74.2k17 gold badges123 silver badges140 bronze badges
1 Comment
Louis
the problem in this solution is if I want to reuse the function with another event, and don't want to preventDefault on this other function. The solution from Kami seems to handle this
lang-js