1

I have a table and I have multiple text boxes in a column.I am add rows to the table using jquery clone method

var row = $('#nameTable tbody>tr:last').clone(true);

Now I want to add ids to the text fields of the new row. Any help?

BackSlash
  • 20,445
  • 19
  • 77
  • 124
Shantanu
  • 181
  • 2
  • 5
  • 15
  • Possible duplicate of http://stackoverflow.com/questions/2977041/changing-various-ids-after-cloning-in-jquery – Wundwin Born Jul 30 '14 at 08:37
  • [Java is to Javascript what Car is to Carpet](http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java): They both start with _java_, but they are completely unrelated. – BackSlash Jul 30 '14 at 08:40

4 Answers4

3

Just add attribute id to that new row object.

  $(row).find('input').attr("id","newId");

Docs of attr()

note: Considering only one input element is there in your new object. If multiple input elements are there, you have to loop on them and have assign individual id's to them, Since Id must be unique.

Suresh Atta
  • 114,879
  • 36
  • 179
  • 284
0

Try with this

var row = $('#nameTable tbody>tr:last').clone(true);
row.find("#someInput").attr("id","someInput_2");
Wundwin Born
  • 3,279
  • 17
  • 35
0

Since you referring to text fields you can make use of this selector

$(row).find('input[type=text]').each(function(element) {
    element.attr("id","newId");
})
V31
  • 7,480
  • 3
  • 24
  • 43
0

For set multiple IDs use (be careful, because ID must be unique):

var row = $('#nameTable tbody>tr:last').clone(true);

row.find(':text').each(function () {
    this.id = 'new_id' + Math.random();
});

I prefer access to ID via pure javascript instead of jquery http://jsperf.com/browser-diet-this-attr-id-vs-this-id/12