X
Publisher: Sigit purnomo kangsigit.com@gmail.com

How to Change HTML ID Specified by .innerHTML

You can use this trick for some purposes with various models, such as onclick, onmouseover, and onload.
  • Materials required is quite simple, as the main engine we use Javascript like this
function myFunction() {
    document.getElementById("demo").innerHTML = "Your text here";
}
  • Then specify the location where the article will appear with this code
<div id="demo"></div>
  • The final step, we create a trigger for call ID demo with code like this
<button type="button" onclick="myFunction()">Try it</button>

Several options for modification

To the above it will only work if the trigger button is pressed.
Replace the code with the onMouseOver="myFunction()" if it wants to produce with a touch mouse (highlight)

Advertisement


If you want to do it automatically after the page has loaded, please use onload="myFunction()" in the <body>
examples: <body onload="myFunction();"> blablabla </body>

For the full code, please try this one
<!DOCTYPE html>
<html>
<head>

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Your text here";
}
</script>

</head>
<body>

<div id="demo">Before..</div>
<button type="button" onclick="myFunction()">Test</button>

</body>
</html> 

Advertisement

How to Change HTML ID Specified by .innerHTML
Add Comments
0 Comments
Click Here to Add Comments

No Comment