0

I have a dynamic table where I add rows dynamically when the user clicks a button.

My button call the following function :

<input type="button" value="Add" onclick="addRowTuy($('#AmonttableTuy tbody'), 'AmontTuy');" />

My function adds a new rows to my table. It include text fields, inputs and select.

I have no problem for the creation of new inputs and text, but for the select, I want copy an existing select and change its id. That's my problem.

This is the function where I create a new rows in my table:

function addRowTuy(table, value)
{
var nbrow = table.children().size();
    table.append( "<tr>"+
    "<td>"+(nbrow+1)+"</td>"+
    "<td><input id='"+value+(nbrow+1)+"Diam"+"'/></td>"+
    "<td><input id='"+value+(nbrow+1)+"Long"+"'/></td>"+
    "<td>"+$("#AmontTuy1Type")+"</td>"+ //This is where I want copy the select element
    "<td><input id='"+value+(nbrow+1)+"Rugo"+"'/></td>"+
"</tr>");
}

Have you an idea on the way to do this?

Etienne
  • 408
  • 2
  • 9
  • 28

3 Answers3

2

Something like this should work

"<td>"+$("#AmontTuy1Type").clone().attr('id', 'newID').html() + 

You need to clone and add its html to your string, not the element itself. Or create a td element and append the clone to it.

Update:

Yeah, you're right, html() only takes the options, not the select itself. Try this:

"<td>"+"<select id='newID'>" + $("#AmontTuy1Type").html() + "</select>" +
artm
  • 8,554
  • 3
  • 26
  • 43
  • this copy only the option of the select and not all the select. It is strange because "AmontTuy1Type" is the id of the select – Etienne Oct 23 '14 at 08:56
1

There are different ways to do this. Below links will be helpful for you.

How to add (clone) form fields using jQuery and increment ids and names

jquery clone form fields and increment id

Community
  • 1
  • 1
KiV
  • 2,225
  • 16
  • 18
0

I have done it like this

$("#AmontTuy1Type").parent().clone().attr('id', 'newID').html()

It copy the select well, but the id of this one doesn't change?

Etienne
  • 408
  • 2
  • 9
  • 28