2

I looked around for an answer to this but have been unsuccessful so far. What I have is a div containing radio buttons with titles

<div>
  <label title_id="1" function="update_answer_title" class="pure-radio" for="option-1">
      <input type="radio" name="1" value="1" id="option-1">
      Talking on the phone or sending a text message.
 </label>
 <label title_id="2" function="update_answer_title" class="pure-radio" for="option-2">
      <input type="radio" name="1" value="2" id="option-2">
      Having your keys held in your hand, ready to unlock car and/or provide defense.
 </label>
</div>

Now what I am trying to do is get the text inside a label when that label is clicked. Not the html of the radio button. So inside my onclick listener I am What I have tried so far is:

     evt.target.childNodes[1].valueOf().innerText
     evt.target.childNodes[1].toString()
     JSON.stringify(evt.target.childNodes[1])

And a bunch of other permutations of these attempts. but each time all I get from that value that these are returning is:

[object Text]

How do I convert object Text to normal text?

Thank you!

0

1 Answer 1

1

Well, That was silly, I figured it out, instead of doing

evt.target.childNodes[1] ... 

All I needed to do was get the inner text of the target and that will filter out the html for me automagically so the solution is:

evt.target.innerText

EDIT:

In Response to comments, innerText does not exist in Firefox. So the way to do this is really: evt.target.innerText || evt.target.textContent;

Thank you @Ruslan Osmanov

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

3 Comments

use evt.target.textContent || evt.target.innerText for compatibility
That's right. innerText is not standard, and Firefox doesn't have it.
Firefox has innerText implemented since version 45! It will be probably be safe to drop out textContent after a few more releases.

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.