| | |
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:
Solved Threads: 0
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:
The <p> tags are created by the following js code:
I want to use javascript to extract the text from the first <p> tag. In my code, I wrote:
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!
The code is very simple. I have a web page that contains the following:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<div id="mydiv"> <p>first</p> </div>
The <p> tags are created by the following js code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
paragraph = document.createElement("p"); text = document.createTextNode("first"); paragraph.appendChild(text); 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)
var mydiv = document.getElementById('mydiv'); var p = mydiv.childNodes[0]; var text = p.childNodes[0]; // I tried others like p.text alert(text.nodeValue);
Last edited by beanryu; Aug 8th, 2009 at 1:29 am.
Hi beanryu,
i prepared some document sample for you, which you can used as referenced when you are mapping elements inside your document.
-essential
i prepared some document sample for you, which you can used as referenced when you are mapping elements inside your document.
javascript Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8" standalone="no"?> <?xml-stylesheet type="text/css" href="#css21" media="all"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html id="xhtml10" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- W3Cs Standard Template : XHTML 1.0 Transitional DTD --> <head> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>Walk the DOM</title> <style id="css21" type="text/css" media="all"> /* <![CDATA[ */ html, body { border : none; color : #405060; font : normal normal normal 90%/1.4 Verdana, Helvetica, Arial, sans-serif; height : auto; margin : 0; min-height : 206px; min-width : 176px; padding : 0; text-align : center; width : auto; } body { background-color : #FFFFFF; } h2.logo { border-top : 1px solid #000; border-bottom : 1px solid #000; letter-spacing : 2px; padding : .200em 1em .200em 1em; margin-bottom : 1.500em; background-color : #405060; color : #fff; } div { border : none; margin : 0; padding : 0; } div#main { margin : 0 auto; background-color : transparent !important; height : 100%; width : 100%; } div.pad { padding : 1.200em; } div.bordered { min-height : 600px; border : 1px solid; padding : 1em; text-align : left; } .no-top-margin { margin-top : 0; } /* ]]> */ </style> <script type="text/javascript"> // <![CDATA[ var dom = function() { var mydiv = document.getElementsByTagName("div")[ "mydiv" ]; if( "createElement" in document ) { // See if the document model supports the "createElement" method. var paragraph = document.createElement( "p" ); // dynamic paragraph. var p = mydiv.getElementsByTagName( paragraph.nodeName ); /* 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 ). */ paragraph.appendChild( document.createTextNode( "First Paragraph")); mydiv.insertBefore( paragraph, p[ 0 ] ); /* instructing our main div to insert our created paragraph right before the second paragraph--which is preloaded on the document. */ 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. There are many different methods or different techniques, that you can apply to extract all the elements you need inside your documents'. */ for( var x = 0; p[ x ]; x++ ) { // Here's a little demo of extracting only the text content of your paragraph collection. for( var y = 0; ( nodes = p[ x ].childNodes[ y ] ); y++ ) { if ( nodes.nodeType === 3 || nodes.nodeType === Node.TEXT_NODE ) { alert( nodes.nodeValue + "\n\n ~ looping the nodeList item." ); continue; } } } } }; window.onload = dom; // ]]> </script> </head> <body id="xhtml-10-transitional"> <div id="main"> <div class="pad"> <div class="bordered"> <h2 class="no-top-margin logo">JavaScript Live Demo!</h2> <!-- 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 ). --> <div id="mydiv"> <p id="Two">Second Paragraph</p> <p id="Three">Third Paragraph</p> </div> </div> <!-- class :: bordered --> </div> <!-- class :: pad --> </div> <!-- id :: main --> </body> </html>
-essential
•
•
Join Date: Aug 2009
Posts: 1
Reputation:
Solved Threads: 1
The following works:
but only if it follows after
because if you run the code before the div is created the script won't find it. So, where are you running the script?
<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>
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
Since you have the web page (web site), can't you:
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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<div id="mydiv"> <p id="mydiv_p1">first</p> </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.
•
•
Join Date: Feb 2009
Posts: 13
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
•
•
•
•
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.
Beanryu,
You said in your original post:
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
Now instead of
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
You said in your original post:
•
•
•
•
The <p> tags are created by the following js code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
paragraph = document.createElement("p"); text = document.createTextNode("first"); paragraph.appendChild(text); document.getElementById('mydiv').appendChild(paragraph);
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!
![]() |
Similar Threads
- How to extract value from a text file (Python)
- How do I extract text from a string? (Python)
- Extract href attribute and value from <a> tag (Python)
- Extract everything between a <span> tag ignoring any othe tag , newline , comma in it (Perl)
- Reading a web page and copying its text to a .txt file (Python)
- Loading text\skipping "markers" (C++)
- parsing html (Java)
- Tkinter Text tags (Python)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Add to favrite on all browsers
- Next Thread: object required error
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate array automatically beta box browser bug calendar captchaformproblem cart close codes column css cursor date debugger decimal dependent design disablefirebug dom download dropdown element embed engine enter error events ext file firefox focus form frameworks getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 iframe images index java javascript javascripthelp2020 jawascriptruntimeerror jquery jsp libcurl listbox maps masterpage media menu microsoft mimic mp4 onmouseoutdivproblem onmouseover paypal pdf php player position post problem programming prototype redirect regex safari scale scriptlets scroll search security select software sql text textarea unicode w3c website window windowofwords windowsxp





