0

I am trying to create html code like this in js

This is html code how i want to get with javascript

   <div id="windwo">
      <div id="windowhead">
      </div>
  </div>

And this is Javascript code test

   var div = document.createElement('div');
        div.id = 'window';
        var div = document.createElement('div');
        div.id = 'windowhead';
        document.body.appendChild(div);
        document.body.appendChild(div);

And out put of javascript code is

<div id="windowhead"></div>

Someone can tell me which i mistake done ?

3 Answers 3

2

You need two DIV variables and to append the second DIV to the first:

var div = document.createElement('div');
div.id = 'window';

var div2 = document.createElement('div');
div2.id = 'windowhead';
div.appendChild(div2);
document.body.appendChild(div);

You were essentially overwriting the first DIV with the second DIV.

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

1 Comment

output is like this <div id="windwo"></div> <div id="windowhead"> </div>
1
var $widow = $('<div>', { id: "widow" });
$widow.append( $('<div>', { id: "widowhead"}) );
$('body').append( $widow );

Comments

0

You are appending the same element twice. so just Change your JS as follows

    var div1 = document.createElement('div');
    div1.id = 'window';
    var div2 = document.createElement('div');
    div2.id = 'windowhead';
    document.body.appendChild(div1);
    div1.appendChild(div2);

3 Comments

I didn't down vote, but there is an additional issue with the append lines.
yes its working both but i didnt understand document.body.appendChild(div1); div1.appendChild(div2); this first is document.body...;and why second is div1.ap...; i am new in js if i get some explantion hmmm jai bhart
it means that first, you have to add div1 to the body, then add div2 to div1, since you want div2 to be inside div1..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.