JS DOM equivalent of jQuery append
5 answers
I think you need to extend the innerHTML property to do this
element[0].innerHTML += "<ul><li><a href='url'></a></li></ul>";
some explanation:
- [0] is necessary because
element
is a collection - + = extend innerHTML and don't overwrite
- closing
</a>
necessary as some browsers only allow valid html for innerHTML
+20
from jQuery source code:
append: function() { return this.domManip(arguments, true, function( elem ) { if ( this.nodeType === 1 ) { this.appendChild( elem ); //<==== } }); },
Please note that for it to work, you need to build a DOM element from a string, this is done using the jQuery domManip
function.
jQuery 1.7.2 source code
+6
The easiest way to replicate the jquery append method in pure JavaScript is through "insertAdjacentHTML"
var this_div = document.getElementById('your_element_id'); this_div.insertAdjacentHTML('beforeend','<b>Any Content</b>');
More Information - MDN insertAdjacentHTML
0