Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically changing syntax using highlightjs

var d;

$("#ing").keyup(function(){

    d= $("#ing").val();

    $("#lipu").html(d);
});

This is the code i am using to change the text inside the div dynamically:

hljs.initHighlightingOnLoad(); 

This line of code helps in syntax highlighting when the code is hardcoded in the tags and works perfectly when page loads.

I want to do the exact same thing when I write the text inside the textbox and the highlighted text be displayed inside the div

I have tried to use following functions

hljs.initHighlighting.called = false;

hljs.initHighlighting();

But it did not work...

Please help I know there is a very simple solution but I couldn't find it on internet....

like image 484
Uzumaki Naruto Avatar asked Feb 16 '26 12:02

Uzumaki Naruto


1 Answers

According to the doc : http://highlightjs.org/usage/ this code should work :

$("#ing").on("input", function(){ // Always prefer the 'input' event instead of keyup

    hljs.highlightBlock($("#lipu").html($(this).val()).get(0));

});

OR (to be clearer)

$("#ing").on("input", function(){ // Always prefer the 'input' event instead of keyup
    $("#lipu").html($(this).val());
    hljs.highlightBlock($("#lipu").get(0)); // highlightBlock expect a DOM element
});
like image 129
Oliboy50 Avatar answered Feb 19 '26 03:02

Oliboy50



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!