1
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

3
  • 2
    wrap it with another div ? why does it have to be 1 ? Commented Jun 20, 2014 at 14:06
  • jsfiddle.net/268BT/1 Commented Jun 20, 2014 at 14:13
  • When later I do somehtml.appendTo($fragment) - it appends it to every element. So I only want one. I'll try single element wrapper now... Commented Jun 20, 2014 at 14:13

1 Answer 1

2

just wrap it with another div.

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.