tags:

views:

44

answers:

4

Is it possible to create a jquery object that I can perform jquery functions on from just a string representation?

I.e.

var item = '<div>hello</div>';
???
alert(item.html());

I think I could do it by adding it to the DOM using append(), then reselecting it, but i dont really want to do that as it seems horribly ineffcient.

EDIT: Thanks for all the replies

+3  A: 

Try this:

var item = '<div>hello</div>';
alert($(item).html());

However the html() function displays the inner html of the object created.

kgiannakakis
A: 
var item = $.create('<div>hello</div>');

try that?

Alex
+5  A: 
Neurofluxation
A: 

Try:

var item = $('<div>');
item.text('hello');

alert(item.text);

item is not a part of the DOM, you can manipuate it the way you like and append it where you like when you feel like it.

thomasmalt