1

I want to show the content of this <pre> tag using alert():

<pre> This is the IRNIC Whois server v1.6.2.<br> Available on web at http://whois.nic.ir/<br> Find the terms and conditions of use on http://www.nic.ir/<br> <br> This server uses UTF-8 as the encoding for requests and responses.<br><br> NOTE: This output has been filtered.<br><br> Information related to 'shirzadi.ir'<br><br><br>domain:       shirzadi.ir<br>ascii:       shirzadi.ir<br>remarks: (Domain Holder) Ehsan Shirzadi<br>remarks:  (Domain Holder Address) Ferdowsi Blv , Mahdi 13 ST, Mashhad, Khorasan razavi, IR<br>holder-c:   es372-irnic<br>admin-c: es372-irnic<br>tech-c:      to52-irnic<br>bill-c:       to52-irnic<br>nserver:  wres1.irpowerweb.com<br>nserver:    wres2.irpowerweb.com<br>last-updated:   2014-01-16<br>expire-date:  2017-10-08<br>source:       IRNIC # Filtered<br><br>nic-hdl:    es372-irnic<br>person:      Ehsan Shirzadi<br>e-mail:       ehsan.shirzadi@gmail.com<br>address:    Ferdowsi Blv , Mahdi 13 ST, Mashhad, Khorasan razavi, IR<br>phone:      +985117688851<br>fax-no:        +989155066372<br>source:        IRNIC # Filtered<br><br>nic-hdl:    to52-irnic<br>org:      Fanavarie Etelaate Towseye Saman (Mihannic)<br>e-mail:      sales@mihannic.com<br>source:       IRNIC # Filtered<br><br></pre>

when I read this content using xx = $('pre').text() and then alert(xx), the is no <br> but when I hard code this content in a variable and alert() I can see them. What is the problem here? finally I want to split the contents by <br>

j08691
  • 204,283
  • 31
  • 260
  • 272
ehsan shirzadi
  • 4,709
  • 16
  • 69
  • 112
  • 3
    use [.html()](http://api.jquery.com/html/), [.text()](http://api.jquery.com/text/) returns the content without tags – Patrick Evans Aug 22 '14 at 13:35
  • Is this the final result you're looking for http://jsfiddle.net/j08691/h4g0usmh/? – j08691 Aug 22 '14 at 13:39
  • when you say "I want to split the contents by
    " do you mean that you want the break tag to break text inside the alert box?
    – grammar Aug 22 '14 at 13:39
  • using `html()` will not work unless the br are replaced as alert does not support tags – GillesC Aug 22 '14 at 13:40

4 Answers4

2

Try $('pre').html() instead of text(). This should preserve < br > as well as other tags / html entities.

Edit

For completeness, as gillesc said, < br > and other tags will be stripped in alert() (since it does not support inner html). Therefore combination of .html() and replace method is required. Newline can be replaced by \n. Full code would look like this:

xx = $('pre').html().replace(/<br>/g, "\n"); 
alert(xx);
kecer
  • 3,031
  • 2
  • 21
  • 24
1

text() will strip the tags out so use html() instead but alert doesn't support tags so you will need to convert your <br/> before sending it to laert

See this post on how to do just that.

HTML Tags in Javascript Alert() method

Community
  • 1
  • 1
GillesC
  • 10,647
  • 3
  • 40
  • 55
1

Using text() keeps only the inner text, not the HTML markup.

Use html() and eventually replace each captured <br /> by the JS new line character, like said here : How replace HTML <br> with newline character "\n"

Community
  • 1
  • 1
-1

Make Like This

 <pre id="MyID">This is Test <br /> This is test</pre>

and javascript code

 <script>
 alert(document.getElementById('MyID').innerHTML);
 </script>
Manisha Patel
  • 354
  • 2
  • 12