By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

how to access value of paragraph tag?

P: 34
i need to access the value of paragraph tag using dom in javascript.

lets say there is <p id="test">this is test para </p>
now how to access the value inside this para ie. "this is test para"?
i have tried:
var text = document.getElementById("test").value, also tried document.getElementById("test").firstChild

but none of them return value of paragraph.

where as in case of textarea i can access this value inside it by
document.getElementById("someId").value

please help me. i need to resolve this very urgently.
thanks in advance.
May 22 '09 #1
Share this Question
Share on Google+
4 Replies


Frinavale
Expert Mod 5K+
P: 9,731
You need to use the innerHTML property to access stuff in the paragraph.
For example:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function showParagraph()
  5. {
  6.    alert(document.getElementById('test').innerHTML);
  7.  
  8. }
  9. </script>
  10. </head>
  11.  
  12. <body>
  13. <p id="test">this is test paragraph</p>
  14. <input type="button" onclick="showParagraph()" value="show paragraph" />
  15. </body>
  16.  
  17. </html>
May 22 '09 #2

Dormilich
Expert Mod 5K+
P: 8,639
the DOM-level-1 method is
Expand|Select|Wrap|Line Numbers
  1. var para = document.getElementById('test');
  2. var text = para.firstChild.nodeValue; // will give the first text node's value
  3.  
  4. // some browsers work with
  5. var text = para.textContent;
the value attribute/property is exclusive for form elements.
May 23 '09 #3

100+
P: 428
If you only care about the text content of a paragraph you can read it with:
Expand|Select|Wrap|Line Numbers
  1. function textvalue(elementreference){
  2.     return elementreference.textContent || elementreference.innerText || '';
  3. }
May 23 '09 #4

P: 1
mrhoo.
Thank you, it worked for me (:
Feb 19 '14 #5

Post your reply

Sign in to post your reply or Sign up for a free account.