2

I need to get an .click() event and prevent form from behaving as it was initially designed and make my own changes and submit. How is it possible?

EDIT: Form input actually has an onclick defined behavior. I need to redefine it somehow.

EDIT: Some code

<form action='link' method='get'>
   <input type="image" name="name" id="id" class="class" onclick="this.form.action='some_link'" title="Title" value="" src="image.gif">
</form>
1

3 Answers 3

5

If you don't want it to submit the form, return false at the end of your click function. If you want to submit the form, use the form element and call the submit function on it:

$('#myFormID').submit();
Sign up to request clarification or add additional context in comments.

Comments

4
$('#form').submit(function(e) {
    // Some code
});

I think it's enough.

2 Comments

So if I have lets say onclick="this.form.action='somelink'" it will execute and afterwards suggested code (Some code) will?
I'm not sure. What is sure is form will be submitted. You can use e.prevendDefault(); and this.submit() if it doesn't work.
0

You could use something like this (haven't tested it but it should work)

form = $("#yourform");
button = $("#yoursubmit");
button.click(function(){
             dosomething();
             form.submit();
             return false;});

2 Comments

What about users who submit form typing enter?
@MatTheCat: The OP specifically asked for a click event. This solution can be applied to a generic button, even if it resides outside the form.

Your Answer

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.