I would like to know how to print script tags in the html with jquery
I would like to know how to print script tags in the html with jquery
You can use the append () method to add it to the end of the content of an element, I'll assume that in your case the div has a id="miDiv"
:
$("#miDiv").append("<script>...</script>");
If you want to replace the text in the div, you can use html () or replaceWith () :
$("#miDiv").html("<script>...</script>");
Even if I do not answer the question I would like to provide a solution without using jQuery for those who might be interested.
It's as simple as creating a script tag with the createElement method and adding the src attribute with the path to your .js file via setAttribute . Then, just add it to the head of your document.
const scriptTag = document.createElement('script')
scriptTag.setAttribute('src', 'http://example.com/tu.script.js')
document.head.appendChild(scriptTag)
A code somewhat more extensive than in the jQuery version but quite readable and equally efficient.
With the .text function of jQuery you can send print html and javascript code.
$("#myDiv").text("<script></script>");
Greetings ...