Reputation: 589
I'd like to embed an html tag in Javascript, between the script> tag
Thanks
Upvotes: 0
Views: 9058
Reputation: 21
Using document.write(), though it is generally advised against the use of this method.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
document.write(`
<html>
<body>
<p>html in script tag</p>
<\/body>
<\/html>
`)
</script>
</body>
</html>
Upvotes: 0
Reputation: 130787
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.
Upvotes: 2
Reputation: 187020
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
Upvotes: 1
Reputation: 1499
Hey maybe you're looking for jQuery.
$("p").text("<b>Some</b> new text.");
See embedded HTML tag.
Write Less, Do More
Upvotes: 4
Reputation: 6468
document.write('
colour1: ' + colour1 + '
colour2: ' + colour2 + '
Upvotes: 0
Reputation: 237010
You can't. If you want the Javascript to write HTML, you'll have to use document.write()
.
Upvotes: 2
Reputation: 24743
You can't do that. tags can contain just text (usually javascript).
Upvotes: 0