-1

So i have a code tag

<code class="language-html" id="highlighting-content">

</code>

I wanted to change the content inside the tag using Javascript, is there any way to do so?

  • `document.getElementById("highlighting-content").textContent = "The text";`, `Object.assign(document.querySelector("#highlighting-content"), { textContent: "The text" });`, etc. There are several ways, all easily found with Google. Familiarize yourself with the [DOM API](//developer.mozilla.org/docs/Web/API/Document_Object_Model) and, if necessary, with [events](//developer.mozilla.org/docs/Web/Guide/Events). – Sebastian Simon Dec 12 '21 at 06:32

1 Answers1

0

code tag is like other html tags, below snippet is just to show an example:

function updateCode(){
  const radnomNum = Math.random();
  const codeEl = document.getElementById('highlighting-content');
  codeEl.innerText= radnomNum;
}

document.getElementById('btn').addEventListener('click' , updateCode);
code {
  color: crimson;
  background-color: #f1f1f1;
  padding:5px
}
<div>The rando num:<code class="language-html" id="highlighting-content">no-num</code></div>
<button id="btn" >update code</button>
Saeed Shamloo
  • 6,199
  • 1
  • 7
  • 18