Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there an option to change the text of a code tag using javascript?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading