How to change a text in html via javascript dynamically?
Posted On : Aug 20, 2020Texts that inside the html elements can be changed dynamically by innerHTML property in javascript. Plain texts can not be changed dynamically.
<html>
<head>
<script>
/* The id of the html element which contains the text to be changed is targeted by document.getElementById("element_id") and new text value in quotes ("") assigned to innerHTML property by equal (=) operator. */
function change_text(){
document.getElementById("myname").innerHTML = "KAAN ÇAMUR";
}
</script>
</head>
<body>
<!-- <h1> html element with "myname" id which contained text to be changed. -->
<h1 id="myname">K. ÇAMUR></h1>
<!-- The name of the function which will be triggered after clicked the button assigned to "onclick" property of button. -->
<input type="button" onclick="change_text()" value="Change the text" />
</body>
</html>