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 ?
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.
<input type="color"> <input type="date"> <input type="datetime-local">
Try this:
document.getElementById('*spaM4').textContent
If you need to target < IE9 then you need to use .innerText
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With