Skip to content

Commit 0a67287

Browse files
CptnFizzbinkevin-brown
authored andcommitted
Answer question about using HTML in selection template
This closes select2#4606.
1 parent 625fc78 commit 0a67287

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

docs/_includes/options/selections/templating.html

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,24 @@ <h3>
2727
I am using HTML in my selection template but it isn't displaying it
2828
</h3>
2929

30-
{% include options/not-written.html %}
30+
<p>
31+
If you want to use HTML in your selection template, you will need to return a jQuery object. Otherwise, Select2 will assume that your template only returns text and will escape it.
32+
</p>
33+
34+
{% highlight js linenos %}
35+
function template(data, container) {
36+
return $('<strong></strong>')
37+
.text(data.text);
38+
}
39+
40+
$('select').select2({
41+
templateSelection: template
42+
});
43+
{% endhighlight %}
3144

3245
<h3>
3346
How can I access the container where the selection is displayed?
3447
</h3>
3548

3649
{% include options/not-written.html %}
37-
</section>
50+
</section>

0 commit comments

Comments
 (0)