Richlewis
Richlewis

Reputation: 15374

Use jquery to select submit button in rails 3

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

Upvotes: 1

Views: 372

Answers (2)

Ram
Ram

Reputation: 144669

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) {
      // ...
   });
});

Upvotes: 1

sgrif
sgrif

Reputation: 3822

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 %>

Upvotes: 1

Related Questions