compress javascript and css files

How to javascript append html string to element

Javascript tutorial, Article for, How to javascript append html string to element. I created following script putting the html at end of body tag.

javascript append html string to element

If you are using or following web 2.0(Table less HTML) standards for creating the HTMLs then this script will work 100%

Just Put this script in your HTML and see the html source. You will able to see the div id named “kapil” with the test content.


<script type=’text/javascript’>
var kapil = document.createElement(‘div’);
kapil.id = ‘form_kapil’;
var all_divs = document.getElementsByTagName(‘div’);
if (document.body.firstChild){
document.body.insertBefore(kapil, all_divs[all_divs.length] );
} else {
document.body.appendChild(kapil);
}
var oldHTML = document.getElementById(‘form_kapil’).innerHTML;
var newHTML = “test content”;
//document.getElementById(‘form_kapil’).innerHTML = newHTML;
var all_divs = document.getElementsByTagName(“div”);
//alert(all_links[all_links.length - 1].innerHTML);
all_divs[all_divs.length - 1].innerHTML = newHTML;
</script>

Have fun!

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.