windsurf88 windsurf88 - 4 months ago 8
Javascript Question

Accessing the label text through JavaScript

Looking access the label text.

I can find the value of the input tag via:



<label for="t">Try this <input type="text" id="t" /></label>
<input type="button" onclick="alert(document.getElementById('t').value)" value="Click me!"/>





But what is the method to access the label's text "Try this" ?

using .innerHTML yields nothing. .parentNode.value yielded undefined.

Answer

<label for="t">Try this <input type="text" id="t" /></label>
<input type="button" onclick="alert(document.getElementById('t').parentNode.innerText)" value="Click me!"/>