Code Kris
Code Kris

Reputation: 447

how to get value of the text box in dynamic table data in jquery

I have created a table that has a text input box. I want to get the value of the text box value within the tr element.how do I do it?

$(document).on('click', '.overtime_send', function(){

    $('#employee_table tr').each(function(row, tr){

      var asd = $(tr).find('td:eq(3)').text();//get the value
      var asd01 = $(tr).find('td:eq(4)').text();//not get the value of the text box
        alert(asd);
  });    

  });

above code with getting all the static content of table row.how do I get the text value into the variable within table data?

Upvotes: 1

Views: 4433

Answers (3)

Nidhin Joseph
Nidhin Joseph

Reputation: 10237

Use the .find('input') since you have just one input in your tr and .val() to get the value.

let your_array = [];

$(document).on('click', '.overtime_send', function() {

  $('#employee_table tbody tr').each(function(row, tr) {

    your_array.push({
      emp_id: $(tr).find('td:eq(0)').text(),
      input_val: $(tr).find('input').val()
    });

  });

});

Upvotes: 1

jishan siddique
jishan siddique

Reputation: 1895

You can try this.

$(document).on('click', '.overtime_send', function(){

    $('#employee_table tbody tr').each(function(row, tr){
      console.log("Get Html "+$(this).find("td:eq(4)").html());
     console.log("Get Text box value"+$(this).closest().find("input[type='text']").val());
  });    

  });

Upvotes: 0

prasanth
prasanth

Reputation: 22510

For 4 td contain input not a text element .so need to find input value instead of text

    $(document).on('click', '.overtime_send', function(){
        $('#employee_table tr').each(function(row, tr){
          var asd = $(tr).find('td:eq(3)').text();//get the value
          var asd01 = $(tr).find('td:eq(4)').find('input').val();// value of input box
            alert(asd);
      });    
});

Upvotes: 2

Related Questions