Reputation: 13080
I have a table:
<table>
<thead>
<tr>
<td>some1</td>
......
<td>some2</td>
<th>count</th>
<th>EditCount</th>
</tr>
</thead>
<tbody>
<tr>
<td>some1</td>
......
<td>some2</td>
<td>25</td>
<td><input type='text' value='1'/></td>
</tr>
<tr>
<td>some1</td>
......
<td>some2</td>
<td>35</td>
<td><input type='text' value='3'/></td>
</tr>
How to get td value in left side from input in each row, for example when user clicked on this input?
Upvotes: 0
Views: 709
Reputation: 19882
Add a class let suppose 'value' to all left side tds then and add another class on inputs call this function
<script type = 'text/javascript'>
$('.button_class').click(function(){
var myvalue = $(this).parent().children().attr(value);
});
this will automaticall select first children or you can use this too
var myvalue = $(this).parent().eq(0).attr(value);
Upvotes: 0
Reputation: 38147
(Note from "left side" i took this to mean the first td
ie the leftmost one)
Try this :
$('input[type=text]' ).change(function() {
var firsttd = $('td:first', $(this).parents('tr'));
});
Uses the .change() method in jQuery (this could be focus
or blur
etc). Uses the selector td:first
to get the first td
and sets the context to the current objects (input') parent
tr`
To get the previous td
(ie with values 25
or 35
use :
$('input[type=text]' ).change(function() {
var prevtd = $(this).parent('td').prev('td');
});
This gets the .parent() td
and then the .prev() td
Upvotes: 3
Reputation: 242
Are you comfortable using jQuery? If so, then you can use the prev() function in jQuery to do get the immediately preceding sibling (the previous td in this case)
Read this : http://api.jquery.com/prev/
Upvotes: 0
Reputation: 2956
Or.. something like this
$('table input').click(function() {
$(this).parent().css('border','1px solid #f00');
});
Although I would personalized the selected with a class or something so it's not that common
Upvotes: 0