0

Considering I want to create this HTML dynamically:

<li><img src="a"/>some text</li>

Some text is a text string that is potentially unsafe, let's say is stored in variable 'some_text'.

The idea is to call $('<li>').append($('<img>').attr({src:"a"}), ... );

Using $(some_text) is bad idea because it's unsafe.

Using text(some_text) doesn't work because the text is not an only child of an element.

I do not want to wrap the text into a <span>

I do not want to invent/use a function that sanitizes or escapes the string

2
  • 1
    Why don't you want to sanitize the text --- I can't imagine that there is any other way to keep from having security issues -- that's why php, angular, and every thing else does sanitize. So? what is your goal and why is a function now a good solution? Commented Jul 11, 2015 at 9:03
  • 1
    Because jQuery has functions that deal with arbitrary text safely (i.e. text()) Commented Jul 11, 2015 at 9:04

1 Answer 1

1

There are many ways, but possibly the simplest is to first add the text content to the li element and then prepend the image to get the correct order.

$('<li>').text(some_text).prepend($('<img>').attr({src:"a"}), ... );
Sign up to request clarification or add additional context in comments.

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.