Access text / template content in javascript

I am trying to access the contents of the html of a script block with the type set to "text / template". I heard about the mechanisms of templates using such tags, but my application is very simple and loading the entire engine is not needed. Can someone shed some light on how I select this item? I think the jQuery.html () function will get the content for me, but I cannot find the element.

The template looks like this:

<script type="text/template" id="repeating-form-section-template">
  <div class="repeating-form-section">
    <label>Field Name:</field>
    <input type="text" value="default value" name="field_name" />
  </div>
</script>

Things I tried:

getElementById('repeating-form-section-template');
$('script').filter(...);
$('#repeating-form-section-template');
$("script[type='text/template']");

Thank!

+3
source share
3 answers

AND

 $('#repeating-form-section-template');
 $("script[type='text/template']");

Work great. Be sure to check them after loading the DOM. Example: http://jsfiddle.net/niklasvh/VKqPX/

+3
source

This should be easy:

http://jsbin.com/evozi5/edit

: http://jsbin.com/evozi5/

tho :

var someVar = $('#repeating-form-section-template').html();

$('#where-i-want-content').append(someVar);

jQuery , . :

<script src="jquery.js"></script>
<script> /* your code */ </script>
<script type="text/template"></script>
+1

innerHTML It is generally considered a bad thing to use, but it is an ideal thing to use here:

var template = document.getElementById("repeating-form-section-template").innerHTML;

document.getElementById('where-you-want-the-content').innerHTML = template;
0
source

All Articles