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?

+3


source to share


3 answers


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

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

      



or

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

      

+5


source


You can create your element dynamically span

using this syntax:



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

      

+1


source


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.

https://jsfiddle.net/g0n71se7/

$("#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.

+1


source







All Articles