0

I have a set a dynamically created divs with the same class name. Now I want to append a entirely new div to all of the above mentioned divs.

Say the class name is extra-upper-block

At the end of my page I have this code

<script>
 //function call to load dynamic content
</script>
<script>
 $('.extra-upper-block').append('<div>New Div</div>');
</script>

This throws an error in chrome's console

Uncaught TypeError: undefined is not a function 

But when this code is executed in chrome's console after the page is loaded, it works! Why doesn't it work even when I load the dynamic content before executing the append command. Help?

asked Jun 27, 2014 at 8:45
0

4 Answers 4

1

Use jQuery class selector.

$(document).ready(function(){
 $('.extra-upper-block').append('<div>New Div</div>');
});

Wrap your code in $(document).ready() for jQuery to get the elements available, and include jQuery file reference.

Note : .append() method is a part of jQuery.

Demo

answered Jun 27, 2014 at 8:49
Sign up to request clarification or add additional context in comments.

1 Comment

enclosing inside $(document).ready() and using jquery selectors fixed the problem. Thanks for the help :)
1

document.getElementsByClassName returns an array-like object, you can't use it like jQuery, you need to access the individual element in a loop. Also, use appendChild on DOM elements, because they don't have an append method (like jQuery does).

Also, you are trying to append a string <div>New div</div>, you can't directly do that with a DOM element, so instead you can create the div element like so:

Demo

var elements = document.getElementsByClassName('extra-upper-block');
for(var i=0; i<elements.length; i++){
 var newDiv = document.createElement('div');
 newDiv.appendChild(document.createTextNode('New div'));
 elements[i].appendChild(newDiv);
}

Note: querySelectorAll has better cross browser support than this. If you have jQuery included you can simply do:

$('extra-upper-block').append('<div>New Div</div>');

As you can see, with jQuery you can append a string directly.

answered Jun 27, 2014 at 8:47

1 Comment

enclosing inside $(document).ready() and using jquery selectors fixed the problem. Thanks for the help :)
0

try writing

document.getElementsByClassName('extra-upper-block')[0].appendChild(document.createElement('div'));
answered Jun 27, 2014 at 8:47

Comments

0

Append is a function in jQuery, try this:

<script>
$(function() {
 $('.extra-upper-block').append('<div>New Div</div>');
}); 
</script>
answered Jun 27, 2014 at 8:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.