Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
140
rated 0 times [  146] [ 6]  / answers: 1 / hits: 59411  / 13 Years ago, mon, november 28, 2011, 12:00:00

I understand so far that in Jquery, with html() function, we can convert HTML into text, for example,



$(#myDiv).html(result);


converts result (which is the html code) into normal text and display it in myDiv.



Now, my question is, is there a way I can simply convert the html and put it into a variable?



for example:



var temp;
temp = html(result);


something like this, of course this does not work, but how can I put the converted into a variable without write it to the screen? Since I'm checking the converted in a loop, thought it's quite and waste of resource if keep writing it to the screen for every single loop.



Edit:



Sorry for the confusion, for example, if result is <p>abc</p> then $(#mydiv).html(result) makes mydiv display abc, which converts html into normal text by removing the <p> tags. So how can I put abc into a variable without doing something like var temp=$(#mydiv).text()?


More From » jquery

 Answers
205

No, the html method doesn't turn HTML code into text, it turns HTML code into DOM elements. The browser will parse the HTML code and create elements from it.



You don't have to put the HTML code into the page to have it parsed into elements, you can do that in an independent element:



var d = $('<div>').html(result);


Now you have a jQuery object that contains a div element that has the elements from the parsed HTML code as children. Or:



var d = $(result);


Now you have a jQuery object that contains the elements from the parsed HTML code.


[#88868] Saturday, November 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
susanajamiep

Total Points: 466
Total Questions: 113
Total Answers: 108

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
susanajamiep questions
Sun, Jun 12, 22, 00:00, 2 Years ago
Mon, Mar 7, 22, 00:00, 2 Years ago
Wed, Jun 10, 20, 00:00, 4 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
;