Skip to content Skip to sidebar Skip to footer

Html Tag Inside Javascript

I am trying to use some HTML tag inside JavaScript, but the HTML tag does not work. How can U use an HTML tag inside JavaScript? I wanted to use h1 but did not work. if (document.g

Solution 1:

You will either have to document.write it or use the Document Object Model:

Example using document.write

<scripttype="text/javascript">if(document.getElementById('number1').checked) {
    document.write("<h1>Hello member</h1>");
}
</script>

Example using DOM

<h1></h1><!-- We are targeting this tag with JS. See code below --><inputtype="checkbox"id="number1"checked /><labelfor="number1">Agree</label><divid="container"><p>Content</p></div><scripttype="text/javascript">window.onload = function() {
    if( document.getElementById('number1').checked ) {
        var h1 = document.createElement("h1");
        h1.appendChild(document.createTextNode("Hello member"));
        document.getElementById("container").appendChild(h1);
    }
}
</script>

Solution 2:

<html><body><inputtype="checkbox"id="number1"onclick="number1();">Number 1</br><pid="h1"></p><scripttype="text/javascript">functionnumber1() {
    if(document.getElementById('number1').checked) {

      document.getElementById("h1").innerHTML = "<h1>Hello member</h1>";
         }
    }
   </script></body></html>

Solution 3:

This is what I used for my countdown clock:

</SCRIPT><centerclass="auto-style19"style="height: 31px"><Fontface="blacksmith"size="large"><strong><SCRIPTLANGUAGE="JavaScript">
var header = "You have <I><fontcolor=red>" 
+ getDaysUntilICD10() + "</font></I>&nbsp; "
header += "days until ICD-10 starts!"
document.write(header)
</SCRIPT>

The HTML inside of my script worked, though I could not explain why.

Solution 4:

here's how to incorporate variables and html tags in document.write also note how you can simply add text between the quotes

document.write("<h1>System Paltform: ", navigator.platform, "</h1>");

Solution 5:

JavaScript is a scripting language, not a HTMLanguage type. It is mainly to do process at background and it needs document.write to display things on browser. Also if your document.write exceeds one line, make sure to put concatenation + at the end of each line.

Example

<scripttype="text/javascript">if(document.getElementById('number1').checked) {
document.write("<h1>Hello" +
"member</h1>");
}
</script>

Post a Comment for "Html Tag Inside Javascript"