4

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
1
  • put to bottom of the event the preventDefault Commented Apr 5, 2013 at 9:52

3 Answers 3

5

Add parameter event to your function and it will be passed by jQuery implicitly.

$('#submitbutton').click(computeUserDimensions);
function computeUserDimensions(event){
 /* computing dimensions */
 event.preventDefault();
}
Kami
19.5k4 gold badges53 silver badges65 bronze badges
answered Apr 5, 2013 at 9:51
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, this works (but with event̀ instead of e`). Thanks!
What is the difference between writing $('#submitbutton').click(computeUserDimensions); function computeUserDimensions(event){ ...} and $('#submitbutton').click(function() { computeUserDimensions(); }); function computeUserDimensions(event){ ...}
In first you are passing reference of defined function and in second you are passing function (anonymous).
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.
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.
2

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

2 Comments

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);});
@user130482 yes (provided prodata is correctly populated array).
1
function computeUserDimensions(e){
 var evt = e ? e:window.event;
 if (evt.preventDefault) evt.preventDefault();
 /* computing dimensions */
}
answered Apr 5, 2013 at 9:52

1 Comment

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.