C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Javascript »

Get the text of label using Javascript


Posted Date: 06 Nov 2008    Resource Type: Code Snippets    Category: Javascript
Author: ProgrammerMember Level: Diamond    
Rating: 1 out of 5Points: 3



This sample code snippet show how to get the text of label using Javascript.

var strMode;

strMode=document.getElementById('lblPayMode').innerText;


The variable strMode returns the text of the label control lblPayMode.




Responses

Author: Amer Sohail    19 Feb 2009Member Level: Bronze   Points : 2
html label control does not contain any value attribute like input controls infact text is written as a text node inside HTML i.e.
Whenever you have to acces or assign this type of text, use innerText property of Node. so following code snippet will be used to acces label's text

document.getElementById('heading').innerText

Above code works great in IE but not in other browsers like Firefox, Safari etc. Reason is that IE provides a non standard facility to get text of a node. DOM specification says that "This is heading" is a child node of type text of label node. so first of all we have to navigate to that child node and then get value of that child node through nodeValue attribute. This will work in each browser including IE and firefox.


document.getElementById("heading").childNodes[0].nodeValue

for more details visit http://sites.google.com/site/technicalinfosite/tips---techniques/howtogetorassignvaluetohtmllabelthroughjavascript



Author: Macolee    19 Feb 2009Member Level: Silver   Points : 1
The innerText property will not work in FF. An asp.net label is replaced by a span tag. we can get the text of label with:
document.getElementById('<% lbl.ClientID %>').innerHTML


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Get the text of label using Javascript  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Retrieve value from popup window using javascript
Previous Resource: Number validation with digit grouping in Javascript
Return to Discussion Resource Index
Post New Resource
Category: Javascript


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use