0

I have this HTML code:

<label id = "lblID"><input type="checkbox"/> some text here</label>

How can I get the text from the label tag using javascript?

1
  • I need to retrieve only the text without input element. Commented Dec 6, 2014 at 20:58

4 Answers 4

3

You probably want to exclude the checkbox

var text=document.getElementById('lblID').textContent;
console.log(text); // some text here

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

Use JavaScript's textContent method. This will extract ONLY the text, and no checkbox.

alert(document.getElementById('lblID').textContent);
<label id = "lblID"><input type="checkbox"/> some text here</label>

Comments

0

To only get the text (not the input), you can use:

document.getElementById('lblID').childNodes[1]

Since your label has 2 children, and the text is the second one.

Comments

-2

By using the "textContent"

var lbltext = document.getElementById('lblID').textContent;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.