I realise this question is similar to some that have been asked before and it's possible I'm not getting it because it's 9pm at night but I'd love to get a little pointer on how to fix the problem. I know where I'm going wrong I just don't know how to fix it:
I'm cloning a table row with 4 select boxes and I want to clone the id's and names but increment the id's by one. This is what i've got at the moment.
$(document).on("click", "#new_item", function() {
var rowCount = $("#widget_location_options tbody>tr:last select").attr("id").match(/\d+/);
var rowCount1 = rowCount*1 + 1;
var row = $("#widget_location_options tbody>tr:last").clone(true);
$("select option:selected", row).attr("selected", false);
$("select", row).attr("name", $("select", row).attr("name").replace(/\d+/, rowCount1) );
$("select", row).attr("id", $("select", row).attr("id").replace(/\d+/, rowCount1) );
row.insertAfter("#widget_location_options tbody>tr:last");
});
The problem is that it's taking the id & name of the first select and correctly incrementing the id but appying it to all selects which isn't what I want.
For clarity the HTML of the row I'm cloning looks like this:
<tr>
<td><select id="arr_widget_location[1][position_id]" name="arr_widget_location[1][position_id]">
<option value="value">label</option>
</select></td>
<td><select id="arr_widget_location[1][position_id]" name="arr_widget_location[1][position_id]">
<option value="value">label</option>
</select></td>
<td><select id="arr_widget_location[1][position_id]" name="arr_widget_location[1][position_id]">
<option value="value">label</option>
</select></td>
<td><select id="arr_widget_location[1][position_id]" name="arr_widget_location[1][position_id]">
<option value="value">label</option>
</select></td></tr>
Hope someone can point out where I'm going horribly wrong!
Many thanks
More in this category... Unique Clone is a jQuery plugin to extend the core clone () method that clones an element while modifying any id and/or for attributes found within the element's scope to append a given suffix. 1. To use the plugin, include the jquery-unique-clone.min.js script after the latest jQuery library.
The first method is .attr (), which takes a parameter as id. The second method is .replace (), which takes two parameters, the string value we want to replace followed by the value to replace. The second parameter in the .replace () is blank. This gives us the number replacing the value div. Here’s another example. This might be useful tool.
To update attribute of element you should use the .attr function. Also, when using this in jQuery context you must use $ (this) syntax. If I understand your intentions so it should be something like that:
The element that you use can be a DIV or an input box. Now, sometimes we might add numbers (numeric values) to the element’s id to make the ids look unique. You can have other reasons too.
@Mark F, you gave me a hint at the right direction - thanks. The actual solution looks like this:
$(document).on("click", "#new_item", function() {
...
$(row).find("select").each(function(){
$(this).attr("name", $(this).attr("name").replace(/\d+/, rowCount1) );
});
$(row).find("select").each(function(){
$(this).attr("id", $(this).attr("id").replace(/\d+/, rowCount1) );
});
...
});
Amazing what a good nights sleep and a nudge in the right direction can achieve.
Thanks again.
you're selecting all select
elements in addition to the row, and changing all of the names and ids.
$("select", row).attr("name", $("select", row).attr("name").replace(/\d+/, rowCount1) );
$("select", row).attr("id", $("select", row).attr("id").replace(/\d+/, rowCount1) );
It looks like you're trying to get each select
in the row you cloned, something like this.
$(row).find("select").attr("name", $("select", row).attr("name").replace(/\d+/, rowCount1) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With