Hello, please tell me how to set the css-class component when it is created dynamically. A piece of code:

tmode = document.createElement('td'); tmode.setAttribute('id', "td_0" + t_index); selector = document.createElement('select'); selector.setAttribute('id', "tmode" + t_index); tmode.appendChild(selector); for (i=0; i<3; i++) { // Добавляем итемы в selector items = document.createElement('option'); items.innerHTML = tmode_values[i]; selector.appendChild(items); } tmode.appendChild(selector); 

Here is what I want to get:

 <select class="basic" id='tmode1'> <option>1</option> <option>2</option> <option>3</option> </select> 
  • one
    selector.setAttribute ("class", "basic"); - Ruslan_K

2 answers 2

To set a class, it’s className to className element's className ( MDN ) property.

 selector.className = "basic"; 

There is also a classList with more powerful functionality - MDN

  • Thank you very much, the problem is solved !!! - fin
  • one
    @fin checked, everything works jsfiddle.net/L1bm01sg - Perkovec

Adding a class to element : element.classList.add('');

In your case like this:
selector.classList.add('basic')