3

I'm not experienced with JS, so please excuse the presumably very beginner question.

I have this line:

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

I need to wrap the "|" in span tags such as '<span class="">|</span>' though when I do so it simply prints out the tags as text as opposed to embedding them as HTML wraps.

How do I go about doing this?

1
  • 2
    Can I ask what it is that this is doing? What's type, where is it defined? What is $el? What's the end-result you're trying to achieve? What you're asking is quite easy, but I'm not convinced that what you're asking is necessarily the best solution to the problem you're trying to solve. Commented May 2, 2015 at 11:34

3 Answers 3

5

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

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

or

$el.html(type).append($('<span/>',{text : '|'}));
Sign up to request clarification or add additional context in comments.

Comments

1

You can dynamically create your span element using this syntax:

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

Comments

1

This fiddle contains my solution. The fiddle uses a button to make it clear what is actually happening. I also added a style to the span so you can see it being added.

https://jsfiddle.net/g0n71se7/

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

By using the jQuery append you will insert content to the end of an element. You could alternatively use prepend to insert at the start of an element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.