Tutorial :embed HTML tag in JavaScript


I'd like to embed an html tag in Javascript, between the script> tag



Is E4X what you're looking for? It allows you to embed XML/XHTML within your JavaScript, like this:

var someXml = <div><b>Some Text</b></div>;  

I doubt that's what you need, but that's the only way you can do what you're asking. Also, I don't think it works in Internet Explorer. Scratch that, it only works in Firefox.

If that's not what you want, use document.write(), as suggested by others.

Edit: E4X is now deprecated and has been removed from newer versions of Firefox. Don't use it. You should use jQuery, as the answer below me suggests, or simply create the elements via document.createElement and friends and inject them into the document.


Hey maybe you're looking for jQuery.

$("p").text("<b>Some</b> new text.");  

See embedded HTML tag.

Write Less, Do More



You can't. If you want the Javascript to write HTML, you'll have to use document.write().


If you write this inside your body tag then also you can access this using your javascript.

If yo want to check whether the document is ready or not then you can use JQuery


You can't do that. tags can contain just text (usually javascript).



colour1: ' + colour1 + '
colour2: ' + colour2 + '


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »