user2728841
user2728841

Reputation: 1427

JQuery(HtmlString) returns multiple elements

var $fragment = $("<div>    <div id=\"Title\" class=\"subheader\"></div></div><div>    <div id=\"leftmenu\"></div><div id=\"formdata\" class=\"databox\" data-fld=\"FormData\"></div></div><div>    <div id=\"okbutton\"></div>    <div id=\"cancelbutton\"></div>    <div id=\"actionmenu\"></div></div>");

$("#x").text($fragment.length);

See this fiddle ...

http://jsfiddle.net/paull3876/268BT/

... loading a JQuery object from a HTML string, creates an array with 3 or 5 elements. I get 5 in visual studio, and 3 in jsfiddle. I think the difference is that the visual studio version has line feeds.

However line feeds, or whatever is causing it, might be important, e.g. in a tooltip "title", so, I'd prefer to have the html preserved and not do any replaces etc on it.

How can I get this HTML to load into a single element, so that $fragment.length = 1 and that single fragment element contains all the elements expressed in the html string ?

thanks

Upvotes: 1

Views: 43

Answers (1)

Dwza
Dwza

Reputation: 6565

just wrap it with another div.

Upvotes: 2

Related Questions