-4

i want to create 2 div and text box in java script like that

<div class="a1">
 <div class="a2">
 <input type="text">
</div>
</div>

is it possible?

markE
  • 102,905
  • 11
  • 164
  • 176
Abhishek Kumar
  • 51
  • 1
  • 10

2 Answers2

1

Yes it's possible:

var div_a1 = document.createElement('div');
div_a1.className = 'a1';
document.getElementsByTagName('body')[0].appendChild(div_a1);


var div_a2 = document.createElement('div');
div_a2.className = 'a2';
div_a1.appendChild(div_a2);


var input = document.createElement('input');
input.type = "text";
div_a2.appendChild(input);

Example: https://jsfiddle.net/5zr1Lget/

Simon Deconde
  • 309
  • 1
  • 4
0

In pure javascript

var div = document.createElement('div');
div.setAttribute('class', 'a1');

var div2 = document.createElement('div');
div2.setAttribute('class', 'a2');
div2.innerHTML = "<input type='text' />";

div.appendChild(div2)
document.body.appendChild(div);
lordkain
  • 3,061
  • 1
  • 13
  • 18
  • 1
    This is almost certainly a duplicate question. Please find a duplicate Q&A rather than adding a duplicate answer. Thanks! – markE Feb 23 '16 at 06:56