Thursday, 16 March 2017

Give params to a div created via document.createElement

I want to create multiple divs using a for loop which i do it like this

for(let i = 0; i < data.length; i++) {
  let divs = document.createElement('div');
  divs.className = 'button';
  divs.data-id = (data[i]).id;                  // error
  document.body.appendChild(divs);
}

Now my issue is that data-id is required but it pops an error. So how can I define data-id dynamically

I also tried giving the whole div as innerHTML of another div but then it wont render. So how can this be solved??



via iamsaksham

No comments:

Post a Comment