jay jay - 1 year ago 89
jQuery Question

How to increment a id when appending elements using jquery?

$("div2").append("<div class=fieldBlock><label >Parameter Name: </label><select type=text id=name2 name=name2 ><option></option></select> </br>");

I have created a button that appends a parameter drop down select when the button is clicked. I want to give each appended select a different id. For example, when button is pressed for first time, that select will have id name2, then if button is pressed again, the new append select will have id name3, and so forth. Any way of doing this?

Answer Source

This shoud work for you.

Fiddle of it working.

var count = 0;
   $(".div2").append("<div class='fieldBlock'><label >Parameter Name: </label><select id='name" + count + "'  name='name" + count + "' ><option></option></select></div> </br>");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download