JQuery add ascending numbers to ID

indexingjquery

I have a form that generates divs. I have applied a base ID to each div but I need to add numbers to them to differentiate between them.

I have read up on Index and looked at other example but I can't get it to work here, it seemed to throw a wobbly when I put the num variable in. Anyway I took out the code that was breaking it.

Here is the fiddle, http://jsfiddle.net/clintongreen/BMX4J/13/

Thanks

Best Answer

val() is not correct here. val() is for input elements.

When you do:

var newDiv = $('<div id="toggleshow" ......

Change it to:

var newDiv = $('<div id="toggleshow_' + num++ + '"

And do var num = 0; outside of the function.