[javascript] 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.getElementById('number1').checked) {
  <h1>Hello member</h1>
}

This question is related to javascript

The answer is


If you write HTML into javascript anywhere, it will think the HTML is written in javascript. The best way to include HTML in JavaScript is to write the HTML code on the page. The browser can't display HTML tags, so the browser will recognize the HTML and write this code in HTML.

document.write("<p>Hello World!</p>");

Though if you would like to write HTML on a function, GetElementById is the best:

function functionName() {
   document.getElementById(" the id of the HTML element to be written in ").innerHTML = "whatever you want to say"
}

Hope this helps!


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

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

This is what I used for my countdown clock:

</SCRIPT>
<center class="auto-style19" style="height: 31px">
<Font face="blacksmith" size="large"><strong>
<SCRIPT LANGUAGE="JavaScript">
var header = "You have <I><font color=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.


<div id="demo"></div>

<input type="submit" name="submit" id="submit" value="Submit" onClick="return empty()">


<script type="text/javascript">
        function empty()
        {
          var x;
          x = document.getElementById("feedbackpost").value;
          if (x == "")
           {
             var demo = document.getElementById("demo");
             demo.innerHTML =document.write='<h1>Hello member</h1>';
              return false;
           };
        }
    </script>

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

Example using document.write

<script type="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 -->
<input type="checkbox" id="number1" checked /><label for="number1">Agree</label>
<div id="container"> <p>Content</p> </div>

<script type="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>

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>");

<html>
 <body>
  <input type="checkbox" id="number1" onclick="number1();">Number 1</br>
  <p id="h1"></p>
   <script type="text/javascript">
    function number1() {
    if(document.getElementById('number1').checked) {

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

<div id="demo"></div>

<script type="text/javascript">
    if(document.getElementById('number1').checked) {
        var demo = document.getElementById("demo");    
        demo.innerHtml='<h1>Hello member</h1>';
    } else {
        demo.innerHtml='';
    }
</script>