Answers for "js string html code in variable"

5

html use js variable as text

<h1>"My number: " <span id="myText"></span></h1>

<script>

	//You can use an IIFE (Immediately Invoked Function Expression)
	(() => {
  		var number = "123";
		document.getElementById("myText").innerHTML = number;
	})();
</script>
Posted by: Guest on September-29-2020
1

javascript variable in html string

let div = document.createElement("div");
div.classList.add("yourClass");
div.setAttribute("onclick", "yourFunction()");
div.setAttribute("onmouseover", "hoverOn()")
div.style.background = "yourColor";
let divInDiv = document.createElement("div");
divInDiv.classList.add("yourClass");
div.appendChild(divInDiv);
Posted by: Guest on June-10-2021

Browse Popular Code Answers by Language