jquery - Is it possible to edit the contents of a <script type="text/template"> with JavaScript? -


here's example of i'm trying edit:

<script id="login-popup" type="text/template">    <h3 id="cover-msg" class="modal-title">you need login that.</h3>` </script> 

i add: class="title" h3 tag. being done via chrome extension, can't control html rendered.

here's caveat: can't assume template same, can't replace or edit entire thing. need able select elements within text , add things needed.

the problem i'm having template seems plain text. can't select #login-popup #cover-msg. please correct me if i'm wrong.

is possible javascript/jquery?

you can follow type of procedure gets text out of script tag, inserts dom element can use dom manipulation on it, gets resulting html out of dom element. allows avoid manual parsing of html text yourself:

var t = document.getelementbyid("login-popup"); var div = document.createelement("div"); div.innerhtml = t.innerhtml; $(div).find("h3").addclass("title"); t.innerhtml = div.innerhtml; 

it follows process:

  1. get innerhtml script tag
  2. create temporary div
  3. puts html temporary div can treat dom elements
  4. using dom query, find <h3>
  5. adds class it
  6. get html out of temporary div
  7. puts html script tag modified version of template.

it works here: http://jsfiddle.net/jfriend00/mqnf1mmp/.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -