inner html using javascript code example

Example 1: change innerhtml

document.getElementById("p1").innerHTML = "New text!";

Example 2: javascript innerhtml

document.getElementById("example").innerHTML = "Paragraph changed!";

Example 3: js innerHTML

document.getElementById("Test").innerHTML = "<p style='color:red;'>Test</p>";

Example 4: execute js with innerhtml

<html>
<head>
 
<script>
function executer_script(monscript) {
    var head = document.getElementsByTagName("head")[0];
    var scr;
 
    var d = document.getElementById('resultat');
    d.innerHTML = monscript;
    var scrajx = d.getElementsByTagName('script');
 
    for(var i in scrajx) {
        scr = document.createElement("script");
        scr.type = "text/javascript";
        scr.text = scrajx[i].text;
        head.appendChild(scr);          
    }
}
</script>
 
</head>
<body>
 
<input type="button" value="test" onclick="executer_script('Mon html + du javascript <script>alert(\'test\');</script>');">
 
<div id="resultat"></div>
 
</body>
</html>

Example 5: innerhtml in javascript

<p id="demo" class="demo_class"></p>

<script>
//simple
document.getElementById("demo").innerHTML = "hello World";

//advance
document.getElementById("demo").innerHTML = person("ahsan", "21");

function person(name1, age1) {
    return ("Your name is " + name1 + ". You're " + age1 + " years old.");
}

</script>