PrismJS / live

Prism Live: Lightweight, extensible editable code editors. A work in progress, try it out at your own risk (and report bugs!) :)
https://live.prismjs.com
MIT License
105 stars 28 forks source link

Adding a code snippet with javascript function does not highlight code but after press spacebar or other key #17

Open hermanvl opened 3 years ago

hermanvl commented 3 years ago

Hello, I am using prism live in a textarea and it works fine but when I try to add a code snippet using a button onclick function (document.getElementById("content").innerHTML += header;), the code is added but invisible until i press spacebar or another key, then it shows up nicely highlighted as expected. Is there a way to force it so it is highlighted right away? It works when I just paste it with ctrl-v but not with button onclick. thanks

dordchn commented 2 years ago

You can trigger the highlighting code by dispatching the 'input' event on the textarea.

textarea.value = code;
textarea.dispatchEvent(new InputEvent("input"));