473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

how to access value of paragraph tag?

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
4 38346
Frinavale
9,735 Expert Mod 8TB
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
8,658 Expert Mod 8TB
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
mrhoo
428 256MB
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
mrhoo.
Thank you, it worked for me (:
Feb 19 '14 #5

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

Similar topics

1
by: Dom Nicholas | last post by:
Hi, I have a table (inside a form) that is dynamically created ie the user can add / remove rows dynamically. Each table row has 4 columns : a selection pull down list a text field for...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
9
by: John Harrison | last post by:
Both gcc 3.3.1 and VC++ 7.1 compile the following code. struct Outer { struct Inner { int f() { return c; } }; private: static const int c;
6
by: Don Sutter | last post by:
Hi all, I'm using Javascript to try to determine the final height of a division so that I can use that value in calculations to set the height of another division and also my body height. I'm...
1
by: rodrigo | last post by:
This is what I am doing public class GridCustomPaging : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid DgSearch;
2
by: Shapper | last post by:
Hello, I have this: Sub Page_Load(Sender As Object, E As EventArgs) Response.Write(Session("culture")) End Sub It displays Session("culture") value. I want to access this value in my HTML...
0
by: Tom_Guus | last post by:
Hello, I have a problem and tried to solve it for two days now without any result. I am lookign for a mechanism to have a listbox with e.g. Customers and a listbox containing the customers...
2
by: RSH | last post by:
I have a situation where I have a page called "HiddenFrame.aspx" that contains a public property exposing the value of a textbox called "TextBox1" that is in a hiddenframe. Loaded in the main...
1
by: vikassawant | last post by:
hi all, Please help me. I declare combo box in one file named as test.java i.e. cmb1 and I want to access value of that combobox in another file. In...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.