18

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()?

hugomg
  • 68,213
  • 24
  • 160
  • 246
eastboundr
  • 1,857
  • 8
  • 28
  • 46
  • 2
    What do you mean by `convert`? `.html()` basically sets the `.innerHTML` of the target element. There is no conversion. – Blender Nov 28 '11 at 17:26
  • What is in / what do you think is in `result`?? – PeeHaa Nov 28 '11 at 17:26
  • 3
    What do you mean by converting HTML into text? I'm very sure if `result` is a string of HTML markup, *the HTML markup* is placed into the `div` *as is*. – BoltClock Nov 28 '11 at 17:26
  • @PeeHaa: A string containing HTML, if I am not mistaken. – Blender Nov 28 '11 at 17:27
  • jquery has `.serialize()`, and the json library has `JSON.stringify()` – Marc B Nov 28 '11 at 17:27
  • @Blender: exactly so thats what you want right? – PeeHaa Nov 28 '11 at 17:27
  • @PeeHaa: The question is vague. I don't have a clue about what the OP is trying to achieve. – Blender Nov 28 '11 at 17:27
  • @Blender: thought you were OP :P – PeeHaa Nov 28 '11 at 17:28
  • Sorry for the confusion, for example, if result is "

    abc

    " then $(#mydiv).html(result) should make mydiv display "abc". which "converts" html into normal text by removing the

    tags. So how can I put "abc" into a variable without doing something like this: var temp=$(#mydiv).text();

    – eastboundr Nov 28 '11 at 17:32
  • 1
    @eastboundr: The `html` method doesn't do any such conversion. The `

    ` tags are not removed, they are parsed into a paragraph element. The element is put into the `mydiv` element, so it's actually the paragraph element that shows the text, not the `mydiv` element.

    – Guffa Nov 28 '11 at 17:35
  • Thanks Guffa, so is there anyway to acquire the String "abc" string without displaying it anywhere on the page? – eastboundr Nov 28 '11 at 17:39
  • @eastboundr: To get the text from that specific string, you can use `var temp = $(result).text();`, i.e. turning the string into elements, then getting the text content from the one element. – Guffa Nov 28 '11 at 18:01

9 Answers9

15

Here is no-jQuery solution:

function htmlToText(html) {
    var temp = document.createElement('div');
    temp.innerHTML = html;
    return temp.textContent; // Or return temp.innerText if you need to return only visible text. It's slower.
}

Works great in IE ≥9.

Finesse
  • 9,793
  • 7
  • 62
  • 92
10

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    I know but it's not clear from the question if he wants the text in the variable as a string or if he wants a jQuery object. – Toni Toni Chopper Nov 28 '11 at 17:33
  • @ToniToniChopper: What did you respond to there, actually? – Guffa Nov 28 '11 at 17:36
  • Sorry, a case of selective blindness. I saw your answer as a comment to my answer and not as a standalone answer. Tired, long day – Toni Toni Chopper Nov 28 '11 at 17:38
  • Thanks Guffa, yes this should work, since we are just setting a variable internally, instead of keep changing the content of a displayed div over 100 times externally. I think this is what I want, I will give it a try. – eastboundr Nov 28 '11 at 17:43
  • @Guffa, Thanks alot " var d = $(result).text(); " kinda works, however, there is still slight difference between their lengths. By which I mean, When I do $("#mydiv").html(result); var c = $("#mydiv").text().length; var d = $(result).text().length; c is "3 characters" longer than d. Very strange, any thoughts? – eastboundr Nov 28 '11 at 18:01
  • I think it has to do with the difference in .text() function alone and .text() after .html(). I will perhaps look into it later. for now it's good enough for the time being. Thanks again. – eastboundr Nov 28 '11 at 18:09
  • I see. You have some spaces around the paragrapgh tag, which will be parsed into textnode elements, and the `text` method concatenates the text from all elements. (But I don't know exactly why this doesn't happen when you put the code into the page.) You would use `$(result).filter('p').text()` to only get the text from the paragraph element. – Guffa Nov 28 '11 at 18:13
  • @Finesse: When jQuery gets outdated, so does this question. Noone will go here looking for a non-jQuery solution. – Guffa Aug 27 '16 at 12:24
9

You could simply strip all HTML tags:

var text = html.replace(/(<([^>]+)>)/g, "");
Crozin
  • 43,890
  • 13
  • 88
  • 135
6

you can try:

var tmp = $("<div>").attr("style","display:none");
var html_text = tmp.html(result).text();
tmp.remove();

But the way with modifying string with regular expression is simpler, because it doesn't use DOM traversal.

You may replace html to text string with regexp like in answer of user Crozin.

P.S. Also you may like the way when <br> is replacing with newline-symbols:

var text = html.replace(/<\s*br[^>]?>/,'\n')
            .replace(/(<([^>]+)>)/g, "");
Larry Foobar
  • 11,092
  • 15
  • 56
  • 89
  • Sorry, I haven't checked my code, so there was a mistake: it was `$("
    ")` instead of `$("div")`. I've edited my answer
    – Larry Foobar Nov 28 '11 at 18:25
4

Why not use .text()

$("#myDiv").html($(result).text());
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • Thanks, but pls note, I'm doing a paragraph trimming and checking, so I'm constantly checking the result in a big loop over many times, so if we do this, myDiv content will be changed over many many times. So I thought is there a way to skip the step to "write" to myDiv, but simply store the result into a variable and checking it "internally" over the loops. – eastboundr Nov 28 '11 at 17:37
0

Easiest, safe solution - use Dom Parser

For more advanced usage - I suggest you try Dompurify

It's cross-browser (and supports Node js). only 19kb gziped

Here is a fiddle I've created that converts HTML to text

const dirty = "Hello <script>in script<\/script> <b>world</b><p> Many other <br/>tags are stripped</p>";
const config = { ALLOWED_TAGS: [''], KEEP_CONTENT: true, USE_PROFILES: { html: true }  };
// Clean HTML string and write into the div
const clean = DOMPurify.sanitize(dirty, config);
document.getElementById('sanitized').innerText = clean;

Input: Hello <script>in script<\/script> <b>world</b><p> Many other <br/>tags are stripped</p>

Output: Hello world Many other tags are stripped

Gal Bracha
  • 19,004
  • 11
  • 72
  • 86
0

Using the dom has several disadvantages. The one not mentioned in the other answers: Media will be loaded, causing network traffic.

I recommend using a regular expression to remove the tags after replacing certain tags like br, p, ol, ul, and headers into \n newlines.

0

var temp = $(your_selector).html();

the variable temp is a string containing the HTML

Toni Toni Chopper
  • 1,833
  • 2
  • 20
  • 29
0

$("#myDiv").html(result); is not formatting text into html code. You can use .html() to do a couple of things.

if you say $("#myDiv").html(); where you are not passing in parameters to the `html()' function then you are "GETTING" the html that is currently in that div element. so you could say,

var whatsInThisDiv = $("#myDiv").html();
console.log(whatsInThisDiv); //will print whatever is nested inside of <div id="myDiv"></div>

if you pass in a parameter with your .html() call you will be setting the html to what is stored inside the variable or string you pass. For instance

var htmlToReplaceCurrent = '<div id="childOfmyDiv">Hi! Im a child.</div>';
$("#myDiv").html(htmlToReplaceCurrent);

That will leave your dom looking like this...

<div id="myDiv">
    <div id="childOfmyDiv">Hi! Im a child.</div>
</div>
Tim Joyce
  • 4,487
  • 5
  • 34
  • 50