Within a Project form, a user can clone any specific task — this is working fine. I now need to replace the copied task’s index ID (which is a time stamp) with a new time stamp. My current code only replaces a single div.
How can I replace all form input/label field IDs with a timestamp? I current do a find() and then replace.
Any ideas greatly appreciated!
$(function() {
$('form').on('click', '.clone-task', function(event) {
event.preventDefault();
var time = new Date().getTime();
var taskCopy = $(this).parents(".dupForm").clone();
$("#dupTask").append(workoutCopy);
var attributes = taskCopy.find('input').attr('name').replace(/\[\d+]/g, time);
alert(attributes);
});
});
Task Form (ruby-on-rails):
<fieldset>
<div class="field">
<b><%= f.label :name, "Major Task" %></b> |
<%= f.text_field :name, :placeholder => "task name" %>
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "remove_fields" %>
</div>
<div class="field">
<%= f.label :time_start, "Scheduled Time" %><br />
<%= f.time_select :time_start, {:ampm => true, :minute_step => 15} %>
</div>
<div class="field">
<%= f.label :task_duration, "Task Duration(mins)" %>
<%= f.number_field :task_duration, size: 3 %>
</div>
<b>Add Supporting Actions to Your Task</b>
<%= f.fields_for :activities do |builder| -%>
<%= render 'activity_fields', f: builder %>
<% end %>
</div>
<%= link_to_add_fields "Add Action", f, :activities %>
</fieldset>
jQuery
attr()method allows you to use a function as argument which will act as a loop to process all matched elements.API refrence: http://api.jquery.com/attr/