0

I am new to jQuery and I am practicing appending div elements. Here is my code:

<!doctype html>
<html lang="en">
<head>
    <title>Div Id</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>

<script>
    document.body.onload = addElement;

    function addElement () { 
        // create a new div element 
        // and give it some content 
        var newDiv = document.createElement("div"); 
        var newContent = document.createTextNode("This is a first division"); 
        newDiv.appendChild(newContent); //add the text node to the newly created div. 

        // add the newly created element and its content into the DOM 
        var currentDiv = document.getElementById("div1"); 
        document.body.insertBefore(newDiv, currentDiv); 
    }
</script>

<body>
    <div id="div1">This is a second division.</div>
</body>
</html>

Output would be:

This is a second division.

But as per my implementation the output should be

This is a first division
This is a second division.

I am not able to figure where it is going wrong. Please someone help me out with this.

Thanks in advance.

2
  • 7
    where is jquery here? you are using plain javascript Commented Sep 29, 2015 at 8:30
  • 1
    FYI the second script tag should either be inside the head element, or just before the </body> tag Commented Sep 29, 2015 at 8:31

4 Answers 4

1

onload is a property of the window object, not the document.body object.

This works:

window.onload = addElement;
Sign up to request clarification or add additional context in comments.

Comments

1

actually it works.... try to move your javascript in the head of you html file

document.body.onload = addElement;

function addElement () { 
  // create a new div element 
  // and give it some content 
  var newDiv = document.createElement("div"); 
  var newContent = document.createTextNode("This is a first division"); 
  newDiv.appendChild(newContent); //add the text node to the newly created div. 

  // add the newly created element and its content into the DOM 
  var currentDiv = document.getElementById("div1"); 
  document.body.insertBefore(newDiv, currentDiv); 
}
<div id="div1">This is a second division.</div>

Comments

0
$('#div1').before('<div id=div0>This is a first division</div>')

demo

If you want Jquery it is as simple as this.

3 Comments

It's a valid solution using jQuery as the Q stated. Get my +1 to mitigate those unreasonable downvotes!
as i shall do to your answer. people just down vote with out even explanation.
Indeed: the Q says "I am new to jQuery and I am practicing appending div elements." so both approaches are totally correct and pertinent.
0

If you want to use jQuery you can check this fiddle:

$('body').prepend('<div>This is a first division</div>');

3 Comments

Stop suggestion jQuery for every baby task
@noa-dev OP wants Jquery it is stated.
@noa-dev OS said "I am new to jQuery and I am practicing appending div elements." so my answer is in that direction.

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.