I’m using simple clone plugin. This is the github page of that plugin.
As of now it generates input name like
user[projects][0][name]
user[projects][1][name]
user[projects][2][name]
Can anyone tell me how to generate name like
user[projects_name][0][1]
user[projects_name][1][1]
user[projects_name][2][1]
I have created a jsfiddle to show you demo.
http://jsfiddle.net/Viruthagiri/YLSdQ/1/
I think this is the function that generates the id.
// append a initial number to elements id, if options[:nested] true, also assign a initial number to the element name
e.find("input, select").each(function(){
var old_id = $(this).attr("id");
$(this).attr("id", old_id+'_0');
if(option.nested == true) {
var old_name = $(this).attr("name");
var reg = /\[\w*\]$/;
var match = reg.exec(old_name);
var new_name = old_name.substr(0, match.index) + "[0]" + match[0];
$(this).attr("name", new_name);
}
})
$.fn.regenerate_names = function(number){
$(this).find("input, select").each(function(){
var old_name = $(this).attr("name");
var old_number = old_name.match(/.*\[(\d+)\]/)[1]; // find the last match
var old_number_index = old_name.lastIndexOf(old_number);
var new_name = old_name.substring(0,old_number_index) + number + old_name.substring(old_number_index+1);
$(this).attr("name", new_name);
});
};
Here is the full source code
I am pake007, the author of the plugin. I have updated the plugin to support your requirement.
Please clone the newest version of simple-clone on github, and for your question, I think you can try the following code:
The new option “start” is used for setting the start of the first array index in the input name, default is 0.
The above code will generate input names like: