Ashish Rajan
Ashish Rajan

Reputation: 1200

How to get input text value from inside td

<tr>
 <td>
      <input type="text" name="desc[]">
 </td>
 <td>
      <input type="text" name="duration[]">
 </td>
 <td>
      <input type="text" name="start[]" class="start">
 </td>
 <td>
       <input type="text" name="wait[]">
 </td>
 <td>
       <input type="text" name="end[]">
 </td>
 <td>
      <input type="text" name="phone[]">
 </td>
</tr>

I have such multiple rows. I need to fetch the values off all tds on keyup event on the td with input name start(and i also need to avoid values of desc[] and phone[]). Since they are many i cannot go with ids, need help in parent child approach with jquery.

I was able to reach parent tr on keyup event on the above mentioned td but not able to access values of input field in other tds. the jquery code i used

$(document).ready(function(){
    $(".start").keyup(function(){
        alert($(this).parent().get(-3).tagName);
    })
});

Upvotes: 19

Views: 144832

Answers (4)

Pajoc
Pajoc

Reputation: 104

Maybe this will help.

var inputVal = $(this).closest('tr').find("td:eq(x) input").val();

Upvotes: 5

Felix Kling
Felix Kling

Reputation: 816272

Ah I think a understand now. Have a look if this really is what you want:

$(".start").keyup(function(){
    $(this).closest('tr').find("input").each(function() {
        alert(this.value)
    });
});

This will give you all input values of a row.

Update:
To get the value of not all elements you can use :not():

$(this).closest('tr').find("input:not([name^=desc][name^=phone])").each(function() {
     alert(this.value)
});

Actually I am not 100% sure whether it works this way, maybe you have to use two nots instead of this one combining both conditions.

Upvotes: 42

mmacaulay
mmacaulay

Reputation: 3029

I'm having a hard time figuring out what exactly you're looking for here, so hope I'm not way off base.

I'm assuming what you mean is that when a keyup event occurs on the input with class "start" you want to get the values of all the inputs in neighbouring <td>s:

    $('.start').keyup(function() {
        var otherInputs = $(this).parents('td').siblings().find('input');

        for(var i = 0; i < otherInputs.length; i++) {
            alert($(otherInputs[i]).val());
        }
        return false;
    });

Upvotes: 1

Andru
Andru

Reputation: 7131

var values = {};
$('td input').each(function(){
  values[$(this).attr('name')] = $(this).val();
}

Haven't tested, but that should do it...

Upvotes: 3

Related Questions