javascript - Rails jquery newbie -


i learning jquery , understand how link js.erb files controller actions , have them work.

i unable determine how button not linked controller action execute jquery commands.

i have following form:

<%= form_for([commentable, comment.new], remote: true, html: { class: "comment-form", id: "comment-form-#{commentable.id}" }) |f| %>   <%= render 'shared/error_messages', object: f.object %>   <%= f.text_area :content, placeholder: "add comment...", rows: 3, minlength: 10, maxlength: 1000 %>   <button class="btn btn-success" type="submit">post comment</button>   <button class="btn btn-default" type="button">cancel</button> <% end %> 

this form displayed using jquery. "cancel" button remove form.

i have created file called activities.js , in have there no response button @ all.

$(this).click(function() {   alert('hooray!'); }); 

you can add id button use selection in onclick() event, code below :

html :

 <button id="cancel_comment" class="btn btn-default" type="button">cancel</button> 

js :

 //handling click in button $(document).ready(function() {     $("#cancel_comment").click(function() {        $(this).parents('form').remove(); //removing parent form     }); }); 

be sure using $(document).ready() .


Comments