Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript jQuery
i have one label and one span.the span is placed inside the label.I want to get only the label text.. how to get the text..
  • <label>labeltext spantext</label>
Posted 8-Feb-12 21:04pm
Comments
SAKryukov at 9-Feb-12 2:23am
   
What part of the Web has banned you? Google? Bing?... :-)
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use the jQuery method .text(), see http://api.jquery.com/text/[^].
 
labelText = $("#myLabel").text()
were the label or any other element is selected by its attribute id:
<label id="myLabel" for="male">Male</label>
 

—SA
  Permalink  
v3
Comments
Abhinav S at 9-Feb-12 2:21am
   
My 5.
SAKryukov at 9-Feb-12 2:22am
   
Thank you, Abhinav.
--SA
thatraja at 9-Feb-12 2:40am
   
5!
Espen Harlinn at 9-Feb-12 8:44am
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

<label id="Label1">labeltext spantext</label>
<script>
  var lbltext = document.getElementById('Label1').innerHTML;
</script>
  Permalink  
v2
Comments
Abhinav S at 9-Feb-12 2:20am
   
My 5. However is this not for setting a label value rather than getting the value?
SAKryukov at 9-Feb-12 2:22am
   
Right, but with little fantasy it could be modified to read :-)
Using jQuery is more elegant, please see my answer. :-)
--SA
Abhinav S at 9-Feb-12 2:27am
   
Yes. Not much of a change while reading. ;)
Yes I did see your answer. It an elegant solution.
thatraja at 9-Feb-12 2:39am
   
Updated
Abhinav S at 9-Feb-12 2:49am
   
Cool.
SAKryukov at 9-Feb-12 2:21am
   
Good enough, a 5, but jQuery is more elegant -- please see my answer.
--SA
Espen Harlinn at 9-Feb-12 8:44am
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try document.getElementsByTagName('label')[0].firstChild.data.
  Permalink  
Comments
SAKryukov at 9-Feb-12 2:25am
   
It can read! A 5.
--SA
Abhinav S at 9-Feb-12 2:28am
   
Thank you SA.
thatraja at 9-Feb-12 2:40am
   
5!
Abhinav S at 9-Feb-12 2:48am
   
Thanks thatraja.
kpkaran88 at 9-Feb-12 3:20am
   
thank you Abinav
using ur code i done that work..
Espen Harlinn at 9-Feb-12 8:37am
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

var amount = document.getElementById("<%=lblAmount.ClientID %>").innerHTML;
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Marcin Kozub 275
1 OriginalGriff 264
2 Sergey Alexandrovich Kryukov 215
3 Praneet Nadkar 197
4 Richard MacCutchan 182
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 26 Sep 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100