Post your trick

A simple trick for adding html code using js

If you want to add a relatively large chunk of html code to the DOM using javascript, and if you find it annoying to write the markup as a string in js, here is a simple trick!

write the mark up normally in the html and use inline display:none to hide it

[html]

….

[html]

now in js (using jquery)

//for inserting dynamic html, insert all the required values in the dummy div first and then assign the inner content to a variable

$(”.htmltoadd>p”).html(’Hello World’);

var myhtml = $(”.htmltoadd”).html();

$(”#required-container”).html(myhtml’);


Post A Trick !

How does "Kodetricks" work?

We at kodeplay like to share knowledge. With Kodetricks, even you can join us. All you need to do is post a programming related trick if you have one or rate a trick if you like someone else's.



Find us on

Technology

Contact Us