java java - 6 months ago 37
jQuery Question

Concatenate with variable in jquery append-function

How could i concatenate a variable in the append-function in jquery?

I have a loop where I dynamically creates checkboxes in a html-table. And every checkbox needs a unique id since I attach a listener to each of them after the loop.

What I want is to add the increment variable in the for loop (variable i)
so in every loop the id is concatenated with variable i that is

loop 0, name should be cb0
loop 1, name should be cb1
and so on..

here is what I've tried

$('<td valign="top"></td>').append('<input type="checkbox" id=\"cb\" ' + cnt + '>').appendTo(row);

where cnt is the variable that increments with an integer in each loop

Something is wrong with the concatenation here - greatful for help


Seems you have closed the quote early i.e. it is not adding the cnt variable.


$('<td valign="top"></td>').append('<input type="checkbox" id=\"cb' + cnt + '\">').appendTo(row);

Instead of

$('<td valign="top"></td>').append('<input type="checkbox" id=\"cb\" ' + cnt + '>').appendTo(row);