Using HTML in jQuery string

I have no experience with JS, so please excuse me, probably very newbie.

I have this line:

$el.text(type + "|");


I need to wrap the tags "|"

in span tags, for example '<span class="">|</span>'

, although when I do it it just prints the tags as text, not embed them in HTML covers.

How should I do it?


source to share

3 answers

Use to print HTML content, text prints plain text. .html()

$el.html(type + "<span class=''>|</span>");



$el.html(type).append($('<span/>',{text : '|'}));




You can create your element dynamically span

using this syntax:

var $el = $('<span>').html('|');




This script contains my solution. The violin uses a button to understand what's really going on. I've also added style to the range so you can see it added.

$("#test").append("<span> | <span>");


Using jQuery append you are inserting content at the end of the element. Alternatively, you can use prepend to insert at the beginning of the element.



All Articles