3

jQUery prevent form submission.

 $('#edit_user_form').submit(function(){
        $("textarea[name = user_form_snippet]").enabled = enabled;
        return false;
        e.preventDefault();
 });

Below is my rails form.

<%form_for @user, :html {:id => "edit_user_form" },
          :url => {action => "update", :id => @user.id} do |f| %>

<% end %>

My jQuery function is getting called, but for strange reason the form is getting submitted even if use either return false or e.preventDefault()

1 Answer 1

7

If you want to use e.preventDefault(), you must have it before your return statement, as the return is always the last thing that happens in a function.

You should also update your code to include the e in the anonymous function, otherwise e.preventDefault() doesn't know what e is :)

http://jsfiddle.net/UEK7d/ <- Example fiddle

Sign up to request clarification or add additional context in comments.

Comments

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.