Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get label text value form a html page?

Tags:

javascript

I have a html page , where the html is rendered as :

<div id = 'mViB'> <table id = 'myTable'> <tbody> <tr> ...</tr> <tr>...</tr> <tr> ...</tr> <tr>....</tr> <tr> <td> <label id="*spaM4" for="*zigField4"> All hell. <span class = 'msde32'></span> </label> </td> </tr> </tbody> </table> </div> 

Now what i want to do is get the label text 'All hell.' from the label.

For that purpose i have used both : document.getElementById('*spaM4').text and document.getElementById('*spaM4').value but incidentally none of them worked.

I have used document.getElementById('*spaM4').innerHTML but that returns the span class as well, but i just want to get the text .

Unfortunately, the asterisks in element IDs are 3rd party code and I cannot change it.

Can any one suggest any other way for getting the label text ?

like image 803
The Dark Knight Avatar asked Jul 30 '13 09:07

The Dark Knight


People also ask

How do I get text from labels in HTML?

Use the textContent property to get the text of a label element, e.g. const text = label. textContent . The textContent property will return the text content of the label and its descendants. If the element is empty, an empty string is returned.

Can a label have a value HTML?

<input type="color"> <input type="date"> <input type="datetime-local">


1 Answers

Try this:

document.getElementById('*spaM4').textContent 

If you need to target < IE9 then you need to use .innerText

like image 75
Niklas Avatar answered Oct 16 '22 16:10

Niklas