1

I have this code

<input type="checkbox"/>
<span>Foobar</span>
<input type="radio" checked="checked">Foo
<input type="radio">Bar

How to get the text Foo with jQuery where $(this) is the node <input type="checkbox"/>?
I can get the <input type="radio" checked="checked"> with $(this).parent().find(":checked").not($(this)) but I don't know how to have the text.

9
  • This isn't correctly formed HTML so you might have trouble Commented Jan 9, 2012 at 14:35
  • @m.edmondson: What isn't correct in the HTML? Commented Jan 9, 2012 at 14:36
  • The <input> aren't closed they should be <input type="radio" checked="checked" />Foo or <input type="radio" checked="checked">Foo</input> or am I mistaken? Commented Jan 9, 2012 at 14:37
  • @m.edmondson: The first example in your comment is only required for XHTML validation. The second isn't actually valid HTML. Commented Jan 9, 2012 at 14:39
  • 1
    I agree - I stand corrected w3.org/TR/html401/interact/forms.html#h-17.4, specifically that End tag: forbidden Commented Jan 9, 2012 at 14:45

2 Answers 2

1

Once you get to the target element...

  • use [0] to get the DOM element at the first index from the jQuery object,

  • use nextSibling to get the text node,

  • use data to extract the text content.


var txt = $(this).next().next()[0].nextSibling.data

or

var txt = $(this).nextAll(':radio')[0].nextSibling.data
Sign up to request clarification or add additional context in comments.

2 Comments

Ahaha, again. Was just typing this ;)
I want the text of the checked radio button so I change :radio by :checked $(this).nextAll(':checked')[0].nextSibling.data Thanks
0

It looks like .val() might work (API Reference).

$(this).parent().find(":checked").not($(this)).val()

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.