-1

I need to reach the the span tag with class = ustatus and have the jQuery object obtained for id = foo.

<tr>
  <td> <input name = 'user' type='radio 'id='foo'/> </td>
  <td> <input id='boo'/> </td>
  <td> <input id='too'/> </td>
  <td> <input id='yoo'/> </td>
  <td> <span class='ustatus'> Active </span> </td>
</tr>

I was trying like:

$("input[name = 'user']:checked").closest('td').find('.ustatus').first().val()

but get undefined as the value. Where did I make a mistake?

Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328

4 Answers4

2

You need to use

$("input[name = 'user']:checked").closest('tr').find('.ustatus:first').text()

Because,ustatus is the sibling of your parenent td. So you need to traverse to the parent tr first. Then you can use the find() method, which will search for the child elements

Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
2

Few Issues in your code:

1) You need to traverse to tr instead of td. as span lies in one of the sibling td of selected radio buttons parent td

2) span element have text/html property associated to it and not value.you need to use .text()/.html() instead of .val()

3) as there is only one ustatus element in tr, you do not need .first() selector.

$("input[name='user']:checked").closest('tr').find('.ustatus').text()
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

span has no value you need to use text() or html()

var text = $("input[name = 'user']:checked").parent().parent().find('.ustatus').text();

or

var html = $("input[name = 'user']:checked").parent().parent().find('.ustatus').html() ;

DEMO

Because your anchor is the input you need to go 2 level up to tr where you can find the td that has the class .ustatus so you need to add 2 parent()

Community
  • 1
  • 1
guradio
  • 15,524
  • 4
  • 36
  • 57
0

You are targetting the cell and then trying to find ".ustatus" class of first order to take the html content/value, using val()

$("input[name = 'user']:checked").closest('td').find('.ustatus').first().val() 

val() is used to get values of form fields. Use either text() or html() to get plain text content or including tags respectively.

$("input[name ='user']:checked").closest('tr').find('.ustatus:first').text()

would work as we are finding .ustatus:first in row and taking the content using text().

Shehary
  • 9,926
  • 10
  • 42
  • 71
ajay-annie
  • 13
  • 5