views:

119

answers:

4

Well after countless tries i can't get this 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 to the right direction on what might be wrong? thanks in advance

regards

A: 

Try:

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

at the appropriate spot in your code. Same thing for ID.

Ken Redler
+2  A: 

Notice that it should be lowercase

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

.html

and

$(this).parent('tr')

is null, the input can't have a parent TR

alternatively to this you can use

.closest( selector, [ context ] ) function

BrunoLM
how would i then in a proper way climb up the dom tree to where the tr is then?
Breezer
To find the nearest enclosing tr, use .closest('tr'), which will do just that -- climb until it's found.
Ken Redler
`closest` is better than `parents` because it will return only one element, if you had a table inside another the `parents` method would bring wrong results. See http://api.jquery.com/closest/ for a comparative
BrunoLM
+1, I didn't know about `closest()`. How useful.
Alastair Pitts
A: 

Your code looks right for the most part, just fix these things:

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

To this:

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

And this:

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

To this:

$(this).parents('tr').attr('id');
bobthabuilda
can't get this working either
Breezer
A: 

You want to use the .parents() function rather than the .parent().

The reason, from the documentation, my highlighting.

Given a jQuery object that represents a set of DOM elements, the .parent() method allows us to search through the parents of these elements in the DOM tree and construct a new jQuery object from the matching elements. The .parents() and .parent() methods are similar, except that the latter only travels a single level up the DOM tree.

Alastair Pitts
cant get it working =/
Breezer
And the reason was the captitalised `.HTML`. But the accepted answer is the best.
Alastair Pitts
@Alastair Pitts - Not only was the .HTML in caps, but the .focusout wasn't working in either Firefox or Opera.
Gert G
@Gert G, that surprising. I thought that all of the jQuery functions worked in pretty much every browser.
Alastair Pitts
nope none of thoose was actually the fault the main reason to why i didnt worked i hadnt capsled the script in$(function () {<--script here-->});
Breezer