-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?

7
  • I presume, you want to create text-box and div's on click of a button? Commented Feb 23, 2016 at 6:50
  • no i just want to create that structure using javascript Commented Feb 23, 2016 at 6:51
  • 2
    Yes. It is possible. Commented Feb 23, 2016 at 6:53
  • I'm removing the html5-canvas tag since your question has nothing to do with html5 canvas. Google javascript document.createElement for more information about using javascript to create your html elements. Commented Feb 23, 2016 at 6:53
  • Have you Googled “How to create an element in JavaScript”? You could have found document.createElement. Commented Feb 23, 2016 at 6:54

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

3 Comments

This is almost certainly a duplicate question. Please find a duplicate Q&A rather than adding a duplicate answer. Thanks!
Will do next time! Thanks for the suggestion.
thanks Simon it is working
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);

1 Comment

This is almost certainly a duplicate question. Please find a duplicate Q&A rather than adding a duplicate answer. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.