1

At the moment I am just trying to get jquery to show an alert when my submit button is pressed in my form, I have given it an id

<%= submit_tag "search", :id => 'submitForm' %>

and tried this

$(document).ready(function() {
 $('#submitForm').submit(function(e) {
e.preventDefault();
 alert('clicked');

 });
});

but i get no alert, I have also tried

$(document).ready(function() {
 $(':input#submitForm').submit(function(e) {
 e.preventDefault();
  alert('clicked');

 });

});

but this doesnt work either..is there anything i am doing that is obviously incorrect

Thanks

2 Answers 2

1

The submit event can only be attached to forms, not to submit tags. You need to put the ID on your form.

<%= form_tag "/", id: "submitForm" do %>
  <%= submit_tag "search" %>
<% end %>
Sign up to request clarification or add additional context in comments.

3 Comments

yeah that works :), but what if i have two forms in one form controlled by one submit button, use the same class on each form?
You should never use the same ID more than once on the same page.
yeah i thought so, class name it is then or just two different ids
1

submit event is fired for form elements, not submit buttons, as your element is an input/button element with type of submit, you should select your form element instead:

$(function(){
   $('#theForm').submit(function(e) {
      // ...
   });
});

2 Comments

could i use a click event instead then?
@Richlewis Yes, you can use click event. Just replace submit with click in your code.

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.