1

If I look at the JQuery doc @ http://api.jquery.com/jQuery/#jQuery2, it says

...If a string is passed as the parameter to $(), jQuery examines the string to see if it looks like HTML ...

However, what if I want this behavior to occur all the time? I'm passing in a variable to $(), and I always want the variable to be interpreted as a HTML, not selector.

One way to do it is wrap the variable with a <span> tag, I don't want to add unnecessary DOM to the tree.

3
  • 3
    Can you give an example of a string that jQuery mistakes for a selector? (For the record, I think this is one of the areas where jQuery's API is just horrible -- the $ function does, what, four different things, at least two of which aren't even distinguished by the type of the argument, but still...do you have an example?) Commented Nov 26, 2010 at 22:29
  • 1
    @T.J.: amen brother. I think $ actually does 7 distinct things, 6 of them documented, the other not so much (see the syntax for setting property values on an object literal). What a mess. Commented Nov 27, 2010 at 4:43
  • @Crescent Fresh: Re that last one: OMG. Commented Nov 27, 2010 at 8:29

5 Answers 5

2

The variable will be interpreted as the value it references. So if your variable contains "<span>", you'll get a span element back.

var tag = "<span>";

var $span = $( tag ); // returns a new span DOM element wrapped in a jQuery object

or if you meant that the variable will only contain the tag name "span", then concatenate the brackets.

var tag = "span";

var $span = $( "<" + tag + ">" );

EDIT: jQuery lets you create DOM elements this way. If you wanted to create something else like a text node, you can use a container as you suggested, but you don't need to add it to the DOM.

var txt = "this will be a text node";

var textNode = $( "<span>", {text: txt} ).contents();

If you append textNode to the DOM, you will only be getting the content of the span.

Sign up to request clarification or add additional context in comments.

Comments

1

I believe if you want to be absolutely certain, without having control of the variable's contents, you'll have to do the wrapper thing or use replaceWith or append instead, since those APIs aren't ambiguous. If you can, do the latter, because the wrapper thing can bite you. (For instance, if the variable contains a table row that will ultimately be appended to a table, you can't use a span or div wrapper; but you can't use a table wrapper for something that isn't a table row.)

For instance, instead of

$(varname).appendTo(dest);

you can do

dest.append(varname); // Or $(dest).append(varname) if `dest` isn't already a jQuery instance

...and instead of

$(varname).replaceAll(targets);

you'd do

targets.replaceWith(varname); // Or $(targest).replaceWith(varname)

Comments

1

In JQuery 1.8 (currently in beta), they're introducing $.parseHTML, which is suppose to perform this task.

http://blog.jquery.com/2012/06/22/jquery-1-8-beta-1-see-whats-coming-and-going/

Comments

0

What exactly do you mean with "HTML object" ?

If you mean a string without any HTML-Elements included, you can create a textNode of it and assign this to $() :

$(document.createTextNode(variable))

Comments

0

HTML passed to jQuery will get converted to DOM elements, but only valid HTML. If you pass a tag properly <tag> or hierarchy thereof you'll get converted code. If you pass random text, unbalanced tags or something odd, you'll get a selector of some description.

4 Comments

Unbalanced tags are alright when creating elements in this manner. jQuery treats $('<div>'), $('<div/>') and $('<div></div>') the same.
Individually it allows it, for a single element, and obviously it allows the self-closed (and therefore self-balanced) shortcut <tag />. I meant for more complex structures.
OK, I see what you were saying. Although in the case of unbalanced or mismatched tags, it will still be considered HTML and will use the browser's innerHTML to create the elements. The browser will make corrections as it sees fit. Here's an example of some really bad HTML being passed, and the result the browser provides.
@patrick Aye, an interesting exercise - note the browser differences. IE: <DIV><SPAN>I'm a span<P></P></DIV>; Chrome: <div><span>I'm a span<p></p></span></div>; FF: <div><span>I'm a span</span></div>. All different! My answer is wrong regarding the unbalanced tags, I will strike that. Cheers. :-)

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.