How to extract text from from a <p> tag?

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Feb 2009
Posts: 13
Reputation: beanryu is an unknown quantity at this point 
Solved Threads: 0
beanryu beanryu is offline Offline
Newbie Poster

How to extract text from from a <p> tag?

 
0
  #1
Aug 8th, 2009
I know, this is really a noob question and I don't really know what went wrong.
The code is very simple. I have a web page that contains the following:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div id="mydiv">
  2. <p>first</p>
  3. </div>

The <p> tags are created by the following js code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. paragraph = document.createElement("p");
  2. text = document.createTextNode("first");
  3. paragraph.appendChild(text);
  4. document.getElementById('mydiv').appendChild(paragraph);

I want to use javascript to extract the text from the first <p> tag. In my code, I wrote:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var mydiv = document.getElementById('mydiv');
  2. var p = mydiv.childNodes[0];
  3. var text = p.childNodes[0]; // I tried others like p.text
  4. alert(text.nodeValue);
I tried many other ways to get the text, but I just can't get it. Everytime I get null or blank or Object Text or no pop up at all. I am pretty sure document.getElementById('mydiv') is getting the right div. Can someone please give me an example, please, I feel so stupid because this shouldn't be a problem but I just can't really find a solution. Thank you so much!
Last edited by beanryu; Aug 8th, 2009 at 1:29 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: How to extract text from from a <p> tag?

 
1
  #2
Aug 8th, 2009
Hi beanryu,

i prepared some document sample for you, which you can used as referenced when you are mapping elements inside your document.

  1. <?xml version="1.0" encoding="utf-8" standalone="no"?>
  2. <?xml-stylesheet type="text/css" href="#css21" media="all"?>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  4. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html id="xhtml10" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6. <!-- W3Cs Standard Template : XHTML 1.0 Transitional DTD -->
  7. <head>
  8. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
  9. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  10. <meta http-equiv="Content-Style-Type" content="text/css" />
  11. <meta http-equiv="Content-Script-Type" content="text/javascript" />
  12. <title>Walk the DOM</title>
  13. <style id="css21" type="text/css" media="all">
  14. /* <![CDATA[ */
  15.  
  16. html, body {
  17. border : none;
  18. color : #405060;
  19. font : normal normal normal 90%/1.4 Verdana, Helvetica, Arial, sans-serif;
  20. height : auto;
  21. margin : 0;
  22. min-height : 206px;
  23. min-width : 176px;
  24. padding : 0;
  25. text-align : center;
  26. width : auto; }
  27.  
  28. body { background-color : #FFFFFF; }
  29. h2.logo {
  30. border-top : 1px solid #000;
  31. border-bottom : 1px solid #000;
  32. letter-spacing : 2px;
  33. padding : .200em 1em .200em 1em;
  34. margin-bottom : 1.500em;
  35. background-color : #405060;
  36. color : #fff; }
  37.  
  38. div {
  39. border : none;
  40. margin : 0;
  41. padding : 0; }
  42.  
  43. div#main {
  44. margin : 0 auto;
  45. background-color : transparent !important;
  46. height : 100%;
  47. width : 100%; }
  48.  
  49. div.pad {
  50. padding : 1.200em; }
  51.  
  52. div.bordered {
  53. min-height : 600px;
  54. border : 1px solid;
  55. padding : 1em;
  56. text-align : left; }
  57.  
  58. .no-top-margin { margin-top : 0; }
  59. /* ]]> */
  60. </style>
  61. <script type="text/javascript">
  62. // <![CDATA[
  63.  
  64.  
  65. var dom = function() {
  66. var mydiv = document.getElementsByTagName("div")[ "mydiv" ];
  67. if( "createElement" in document ) { // See if the document model supports the "createElement" method.
  68. var paragraph = document.createElement( "p" ); // dynamic paragraph.
  69.  
  70. var p = mydiv.getElementsByTagName( paragraph.nodeName );
  71. /* our paragraph collections referenced by an index position ( when the dynamic paragraph is appended inside mydiv, then indexPos : 0 - will become the dynamic paragraph, 1 - is the second, and 2 - for the third and last paragraph inside mydiv ). */
  72. paragraph.appendChild( document.createTextNode( "First Paragraph"));
  73.  
  74. mydiv.insertBefore( paragraph, p[ 0 ] );
  75. /* instructing our main div to insert our created paragraph right before the second paragraph--which is preloaded on the document. */
  76. alert( p[ 0 ].innerText + "\n\n ~ using the innerText method" ); /* The simplest way of getting the text-node inside any of the target paragraph is to use the "innerText" method.
  77.  
  78. There are many different methods or different techniques, that you can apply to extract all the elements you need inside your documents'. */
  79.  
  80. for( var x = 0; p[ x ]; x++ ) { // Here's a little demo of extracting only the text content of your paragraph collection.
  81. for( var y = 0; ( nodes = p[ x ].childNodes[ y ] ); y++ ) {
  82. if ( nodes.nodeType === 3 || nodes.nodeType === Node.TEXT_NODE ) {
  83. alert( nodes.nodeValue + "\n\n ~ looping the nodeList item." );
  84. continue;
  85. }
  86. }
  87. }
  88. }
  89. };
  90.  
  91. window.onload = dom;
  92. // ]]>
  93. </script>
  94. </head>
  95. <body id="xhtml-10-transitional">
  96. <div id="main">
  97. <div class="pad">
  98. <div class="bordered">
  99. <h2 class="no-top-margin logo">JavaScript Live Demo!</h2>
  100. <!-- The only thing that is missing here, is our 1st paragraph. Which we will be needing to append inside the div( mydiv ) using our created function ( dom ). -->
  101. <div id="mydiv">
  102.  
  103. <p id="Two">Second Paragraph</p>
  104. <p id="Three">Third Paragraph</p>
  105. </div>
  106.  
  107. </div> <!-- class :: bordered -->
  108. </div> <!-- class :: pad -->
  109. </div> <!-- id :: main -->
  110. </body>
  111. </html>

-essential
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 1
Reputation: quiraang is an unknown quantity at this point 
Solved Threads: 1
quiraang quiraang is offline Offline
Newbie Poster

Re: How to extract text from from a <p> tag?

 
1
  #3
Aug 8th, 2009
The following works:

<script type="text/javascript">
paragraph = document.createElement("p");
text = document.createTextNode("first");
paragraph.appendChild(text);
document.getElementById('mydiv').appendChild(paragraph);
text= document.getElementById('mydiv').innerHTML;
alert(text);
</script>

but only if it follows after
<div id="mydiv">
</div>
because if you run the code before the div is created the script won't find it. So, where are you running the script?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: How to extract text from from a <p> tag?

 
1
  #4
Aug 8th, 2009
Originally Posted by beanryu View Post
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div id="mydiv">
  2. <p>first</p>
  3. </div>
Since you have the web page (web site), can't you:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div id="mydiv">
  2. <p id="mydiv_p1">first</p>
  3. </div>

In other words, have your code that creates the <p> assign an ID to it. Fetching the .innerHTML of that <p> will be trivial once it has an ID.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 13
Reputation: beanryu is an unknown quantity at this point 
Solved Threads: 0
beanryu beanryu is offline Offline
Newbie Poster

Re: How to extract text from from a <p> tag?

 
0
  #5
Aug 9th, 2009
THank YOU ALL so much for the help, especially to essential for the sample code. I found out that the problem was actually google chrome, everything works fine on IE, but nothing works on Chrome, innerText or nodeValue. When I click on google chrome's about thing, it saids it's up to date. Anyone having this problem on chrome? Again Thanks alot.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: How to extract text from from a <p> tag?

 
1
  #6
Aug 9th, 2009
Originally Posted by beanryu View Post
THank YOU ALL so much for the help, especially to essential for the sample code. I found out that the problem was actually google chrome, everything works fine on IE, but nothing works on Chrome, innerText or nodeValue. When I click on google chrome's about thing, it saids it's up to date. Anyone having this problem on chrome? Again Thanks alot.
I would sooner believe you are using a non-standard 'feature' of IE. Try Firefox, Opera and a few other browsers; ask others to try it on Safari and Konqueror. If it still only works in IE, then you are using a feature found only in IE and your application is not cross-browser capable.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 807
Reputation: Airshow is on a distinguished road 
Solved Threads: 114
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: How to extract text from from a <p> tag?

 
1
  #7
Aug 9th, 2009
Beanryu,

You said in your original post:
Originally Posted by beanryu View Post
The <p> tags are created by the following js code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. paragraph = document.createElement("p");
  2. text = document.createTextNode("first");
  3. paragraph.appendChild(text);
  4. document.getElementById('mydiv').appendChild(paragraph);
Therefore, you should have the means for javascript to remember the contents of these paragraphs without needing to retreive them from the DOM.

Create a javascript array var paraContents = []; , then every time you create/populate a para, paraContents['mydiv_n'](txt); .

Now instead of document.getElementById(....) you can just do txt = paraContents['mydiv_n'] .

It's horribly inefficient because there are two copies of each set of para text, but it's paractical if you can't get Chrome to play ball with getElementById. Depending on exactly what problem Chrome is having, you may be able to get away with storing references to the paras, rather than their contents. This will be much more efficient.

Airshow
Last edited by Airshow; Aug 9th, 2009 at 10:47 pm.
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 13
Reputation: beanryu is an unknown quantity at this point 
Solved Threads: 0
beanryu beanryu is offline Offline
Newbie Poster

Re: How to extract text from from a <p> tag?

 
0
  #8
Aug 10th, 2009
hey thanks for the suggestion
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 348
Reputation: Troy III will become famous soon enough Troy III will become famous soon enough 
Solved Threads: 42
Troy III's Avatar
Troy III Troy III is offline Offline
Posting Whiz

Re: How to extract text from from a <p> tag?

 
0
  #9
Aug 13th, 2009
<script type="text/javascript">
paragraph = document.createElement("p");
text = document.createTextNode("first");
paragraph.appendChild(text);
document.getElementById('mydiv').appendChild(paragraph);
alert(text.nodeValue);
</script>
Last edited by Troy III; Aug 13th, 2009 at 9:20 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC