JavaUser JavaUser - 1 year ago 63
jQuery Question

Dynamic id for HTML element which is getting created newly

I am replacing a span inside a table > td with option box.
I want to assign some unique id with dynamic value while creating the option box. How to achieve this?

Unique id is already generated , I want to set this unique id in Html while creation.

<td id="tdTest">
<span id="spanId" class="connectedSlot" style="color:rgb(27,99,173)">Enabled</span>

$("#"+spanId).replaceWith(function () {
return "<select class='form-control' id=<<DYNAMIC ID>> style='width:200px;'><option value='0'>Enabled</option><option value='1'>Disabled</option></select>";

For example , I will get some value from and assign to a variable and later assign this variable to id.

var myId = "OptionId"+test;

select class='form-control' id=myId style='width:200px;'>


Answer Source

Use a variable within a string like this:

var unqiueID = "myID123", ret = "";
ret = "<select class='form-control' id='" + unqiueID +"' style='width:200px;'>";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download