print script tags on a div with jquery

1

I would like to know how to print script tags in the html with jquery

    
asked by Josbert Hernandez 03.08.2016 в 02:06
source

3 answers

3

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>");
    
answered by 03.08.2016 в 02:34
0

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.

    
answered by 03.08.2016 в 08:13
0

With the .text function of jQuery you can send print html and javascript code.

   $("#myDiv").text("<script></script>");

Greetings ...

    
answered by 03.08.2016 в 16:43