unammedkiller
unammedkiller

Reputation: 89

checkbox dynamic create for jquery

How do i create multiple checkbox for jquery to identify it?

example i have a html page with this is call new.html

<div id =new> <input type=checkbox"></div>

at create.html i want to use jquery to load multiple checkbox

<div id=load> <div>
$("#load).load(new.html)

so if i use a for loop to loop 10times to create 10 checkbox, how do i identify each checkbox uniquely?

for(var i = 0;i<10;i++){
$("#load).load(new.html)}

Upvotes: 0

Views: 2497

Answers (1)

elclanrs
elclanrs

Reputation: 94101

Give them unique id, cache your markup in a variable and append everything at last for best performance.

var inputs = [], i
for(i = 0; i < 10; i++)
  inputs.push('<input type="checkbox" id="ck'+ i +'"/>')
$('#load').append(inputs.join(''))

Edit:
Helper function:

var makeCkBoxes = function (n) {
  var inputs = [], i
  for(i = 0; i < n; i++)
    inputs.push('<input type="checkbox" id="ck'+ i +'"')
  return inputs.join('')  
}

$('#load').append(makeCkBoxes(10))

Upvotes: 1

Related Questions