I'm trying to add new fields in a form using jQuery.
This is new.html.erb
<%= form_for @hour do |f| %>
<div id="hourSet">
<%= render partial: "hours_form" %>
</div>
<a href="javascript:;" id="addNewHour">Add Hours</a>
<div class="hide" id="new_hours_form">
<%= render partial: "hours_form", locals: {hour: false} %>
</div>
</div>
js file
$(document).ready ->
$("#addNewHour").on "click", ->
$("#hourSet").append $("#new_hours_form").html()
$("#removeNewHour").on "click", ->
$("#hourSet").remove()
_hours_form.html.erb
<div class="hoursForm">
<%= label_tag 'day' %>
<%= text_field_tag 'day[]' %>
<%= label_tag 'open_time' %>
<%= text_field_tag 'open_time[]' %>
<%= label_tag 'close_time' %>
<%= text_field_tag 'close_time[]' %>
<a href="#" id="removeNewHour">X</a>
</div>
When I'm trying to populate the fields, only the first X works, and it deletes all the fields. I like to only delete 1 at a time. Whats a best method to add unique ids and get those ids for easy removal?
Thanks!
@hour.idonly work if there was an id (for existing data?). Since I'm adding new data, there wouldn't be@hour.idthat exists yet?