$("td input").focusout(fun...">

Finding parent class and id

Well, after countless tries, I can't get this to work?

<script type="text/javascript">
    $("td input").focusout(function() {
      var column = $(this).parent('td').attr('class');
      var row = $(this).parent('tr').attr('id');
      $('#dat').HTML(row+" "+column);
    }); 
</script>

      

And the html looks like this

<tr class="numbers" id="1">
<td class="a" align="right">1</td>
<td class="b"><input class="input" type="text" value=""/></td>
<td class="c"><input class="input" type="text" value=""/></td>

<td class="d"><input class="input" type="text" value=""/></td>
<td class="e"><input class="input" type="text" value=""/></td>
<td class="f">0</td>
<td class="g"><input class="input" type="text" value=""/></td>
</tr>

      

Can anyone point me in the right direction what might be wrong? thanks in advance

considers

+2


a source to share


4 answers


Please note that it must be lowercase

$('#dat').HTML(row+" "+column);

      

.html

and



$(this).parent('tr')

      

is null, input cannot have parent TR

alternatively you can use

.closest( selector, [ context ] )

function

+2


a source


Try:

$(this).closest('td').attr('class')

      



in the appropriate place in your code. The same goes for ID.

0


a source


Your code looks like for the most part, just fix the following things:

$('#dat').HTML(row+' ' +column);

      

For this:

$('#dat').html(row + ' ' + column);

      

And this:

$(this).parent('tr').attr('id');

      

For this:

$(this).parents('tr').attr('id');

      

0


a source


You want to use the function .parents()

, not .parent()

.

The reason, from the documentation, is my highlight.

Given a jQuery object that represents a set of DOM elements, the .parent () method allows us to parent those elements in the DOM tree and construct a new jQuery object from the corresponding elements. The.parents () and .parent () are similar, except that the latter is moved one level up the DOM tree.

0


a source







All Articles