loviji
loviji

Reputation: 13080

the nearest td value

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

Answers (4)

Muhammad Raheel
Muhammad Raheel

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

Manse
Manse

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') parenttr`

Working example here

Update

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

Working example here

Upvotes: 3

S.Raaj Nishanth
S.Raaj Nishanth

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

kernelpanic
kernelpanic

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

Related Questions