Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery clone, replace number in id and name attributes

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

like image 859
BrochanGuMor Avatar asked Jun 05 '13 19:06

BrochanGuMor


People also ask

What is unique clone in jQuery?

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.

How do I replace a string with a number in JavaScript?

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.

How to update attribute of an element using jQuery?

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:

Why do we add numbers to an element’s ID?

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.


2 Answers

@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.

like image 104
BrochanGuMor Avatar answered Nov 15 '22 05:11

BrochanGuMor


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) );
like image 30
Mark F Avatar answered Nov 15 '22 04:11

Mark F