Is there a way to convert HTML into normal text without actually write it to a selector with Jquery?

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

-------------Problems Reply------------

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.

Why not use .text()

$("#myDiv").html($(result).text());

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

the variable temp is a string containing the HTML

$("#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>

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, "");

You could simply strip all HTML tags:

var text = html.replace(/(<([^>]+)>)/g, "");

Category:javascript Views:0 Time:2011-11-28

Related post

  • Convert XML to Plain Text 2009-06-26

    My goal is to build an engine that takes the latest HL7 3.0 CDA documents and make them backward compatible with HL7 2.5 which is a radically different beast. The CDA document is an XML file which when paired with its matching XSL file renders a HTML

  • How can I convert an offset in text segment of an Win32 executable into a pointer at runtime? 2009-08-03

    How can I convert an offset in text segment of an Win32 executable into a pointer at runtime? When using a disassembler, I can see the relative addresses. But how can I convert them to an absolute address at runtime? For example: .text:402BE620 Which

  • Converting HTML to plain text in PHP for e-mail 2009-12-10

    I use TinyMCE to allow minimal formatting of text within my site. From the HTML that's produced, I'd like to convert it to plain text for e-mail. I've been using a class called html2text, but it's really lacking in UTF-8 support, among other things.

  • converting multiple lines of text into a data frame 2010-03-06

    I'm trying to find a way to convert multiple lines of text into a data frame. I'm not sure if there's a way where you can use read.delim() to read in multiple lines of text and create the following data frame with something akin to rehape()?. The dat

  • Convert HTML to Plain Text using c++ 2010-03-09

    I am doing mail parsing application which required to convert the HTML file to Plain Text. regarding this i have found some scripts which does conversion. I want to do same thing in C++. So please suggest me any Cross platform and open source C++ lib

  • What's the fastest way to convert a string of text into a url-safe variable? 2010-04-03

    I'd like to convert a string of text, e.g., "User Name" into something which I can turn into part of a url, e.g., "User-Name." What's the fastest way to do a string replacement ("-" for " ") as well as make sure that characters are only [a-zA-Z0-9]?

  • How to convert Xml files to Text Files 2010-05-31

    I have around 8000 xml files that needs to be converted into text files. The text file must contain title, description and keywords of the xml file without the tags and removing other elements and attributes as well. In other words, i need to create

  • How do i convert a memorystream to text? 2010-07-22

    I am using a tcpstream and copied the data into memorystream. Now i would like to convert it to a text (UTF-8 encoded). I tried various ways and did flush() but i could not figured it out. I tried using it in combination with StreamReader with no luc

  • What kind of char is this and how do I convert it to a text? 2010-08-18

    What kind of char is this and how do I convert it to a text in c#/vb.net? I opened a .dat file in notepad, took a screenshot and attached it here. --------------Solutions------------- Your screenshot looks like the digits "0003" in box. This is a com

  • Converting Wikitext to plain text in Java 2010-09-17

    Any script available for converting Wikitext to Plain text? I prefer it implemented in Java. Thanks! --------------Solutions------------- I guess there would have to be two steps involved: First, convert the Wiki Text two HTML e.g. using Textile Then

  • Convert HTML to plain text in Java 2010-10-12

    I need to convert HTML to plain text. My only requirement of formatting is to retain new lines in the plain text. New lines should be displayed not only in the case of < br > but other tags, eg. < tr/>, < /p> leads to a new line too

  • How to get some elements from html source and convert them to readable text? 2011-01-27

    I have a page which displays "HeLLo 54292" in ASCII art, using + characters inside <table> tags to produce block letters. I'm generating this with PHP. You can check out page's html source code, and see how the ASCII art is constructed. I want

  • Convert html to plain text in VBA 2011-03-16

    I have an Excel sheet with cells containing html. How can I batch convert them to plaintext? At the moment there are so many useless tags and styles. I want to write it from scratch but it will be far easier if I can get the plain text out. I can wri

  • Convert Audio File to text using System.Speech 2011-05-10

    I am looking to convert a .wav file recorded through an android phone at 16000 to text using C#; namely the System.Speech namespace. My code is mentioned below; recognizer.SetInputToWaveFile(Server.MapPath("~/spoken.wav")); recognizer.LoadGrammar(new

  • Need to convert Excel sheets into Text (tab delimited ) as in MS Excel 2007 programatically using C# 2011-08-20

    Ok, here is the problem MS Excel allows me to save the whole sheet as text tab delimited I'm using this text in a program , but the user needs to make the previous step manually I want to automate this step so the user only import the Excel Book into

  • Converting DICOM files to text files 2011-09-19

    I wanted to know if there's any ways i could convert DICOM files to text files(.txt or .doc)? Which would show me the attribute tags, name (if possible, not a ncecessity) , value and VR? Thanks. --------------Solutions------------- The open source DI

  • MD5 convert and convert it back in text 2011-12-05

    i want to save password s in database encrypted in md5 How to convert text into md5 with c# and convert it back in text? Thanks --------------Solutions------------- Hashing is not encryption - it is not reversible. You shouldn't need to convert back

  • Convert HTML to Plain Text while preserving P, BR, UL, OL? 2011-12-07

    During exporting from an HTML text to an Excel sheet, I'm trying to preserve basic formattings like HTML line breaks (<br>, <p>), lists (<ol>, <ul>) etc. Example input: <p>This is a test.</p> <p>This is anoth

  • Convert scientific notation to text and saving in .csv file 2012-04-12

    I have an inventory report in Excel 2007 where the SKU numbers appear in scientific notation (I.E.6.13E+11). The worksheet file is a .CSV file. I know what this notation is and I know the various ways to convert this number to text. My problem is thi

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.157 (s). 11 q(s)