0

I want to select the input fields' values in each column.

<table id="tablo">
     <tr>
         <td><input class="Name" type="text" /></td>
         <td><input class="Age" type="text" /></td>
         <td><input class="No" type="text" /></td>
     </tr>
     <tr>
         <td><input class="Name" type="text" /></td>
         <td><input class="Age" type="text" /></td>
         <td><input class="No" type="text" /></td>
     </tr>
     <tr>
         <td><input class="Name" type="text" /></td>
         <td><input class="Age" type="text" /></td>
         <td><input class="No" type="text" /></td>
     </tr>
     ...
</table>

Then selecting each input value, I will create an object with these values and push the object in an array by looping through the selected inputs:

var arr= []
var obj = {
    Name: jquery selector will come here,
    Age: jquery selector will come here,
    No: jquery selector will come here
}
arr.push(obj);

I've tried with jQuery's $.each() function, but I just only reach the columns.

3
  • 7
    Identifiers in HTML must be unique, Your HTML is invalid. First correct it Commented Jun 19, 2017 at 12:58
  • So loop over and build the objects. Commented Jun 19, 2017 at 13:04
  • A simple fiddle Commented Jun 19, 2017 at 13:47

5 Answers 5

2

Id must be unique, so try it with using class like,

var objs=[];
$('#tablo tr').each(function(){
  var inputs = $(this).find('input'),o={};
  inputs.each(function(){
     o[$(this).attr('class')]=this.value;
  });
  objs.push(o);
});

Snippet,

var objs = [];
$('#tablo tr').each(function() {
  var inputs = $(this).find('input'), o = {};
  inputs.each(function() {
    o[$(this).attr('class')] = this.value;
  });
  objs.push(o);
});
console.log(objs);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="tablo">
  <tr>
    <td><input class="Name" type="text" value="test1"/></td>
    <td><input class="Age" type="text" value="123"/></td>
    <td><input class="No" type="text" value="no1" /></td>
  </tr>
  <tr>
    <td><input class="Name" type="text" /></td>
    <td><input class="Age" type="text" /></td>
    <td><input class="No" type="text" /></td>
  </tr>
  <tr>
    <td><input class="Name" type="text" /></td>
    <td><input class="Age" type="text" /></td>
    <td><input class="No" type="text" /></td>
  </tr>
</table>

Sign up to request clarification or add additional context in comments.

5 Comments

How can I check the null input values?
I've edited the code you sent to check the input values, but didn't work. var objs = []; $('#tablo tr').each(function() { var inputs = $(this).find('input'), o = {}; inputs.each(function() { o[$(this).attr('class')] = this.value; }); if(o['Name'] =! undefined && o['Yas'] =! undefined && o['No'] =! undefined) { ogrenciler.push(o); } objs.push(o); });
Use != instead of =! and o['Age'] in place of o['Yas']
I've fixed it like this, but didn't work again. It pushed the first row's input values but didn't push the others row's input values which either isn't undefined or undefined.
Try to use if(o['Name'] && o['Age'] && o['No']) condition
1

Try this: you can iterate earch row first and then read each input. Note: id must be unique throughout the DOM, hence replaced with classes

$(function(){
  var array = [];
  $('#tablo tr').each(function(){
     var obj = {};
     //obj['name']=$(this).find('td input:eq(0)').val();
     //obj['age']=$(this).find('td input:eq(1)').val();
     //obj['no']=$(this).find('td input:eq(2)').val();

     //for dynamic build of object
     $(this).find('input').each(function(){
         obj[$(this).attr('class')]=$(this).val();
      });
     array.push(obj);
  });
  
  console.log(array);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tablo">
     <tr>
         <td><input class="Name" type="text" value="name1"/></td>
         <td><input class="Age" type="text" value="name1"/></td>
         <td><input class="No" type="text" value="name1"/></td>
     </tr>
     <tr>
         <td><input class="Name" type="text" value="name2"/></td>
         <td><input class="Age" type="text" value="age2"/></td>
         <td><input class="No" type="text" value="no2"/></td>
     </tr>
     <tr>
         <td><input class="Name" type="text" value="name3"/></td>
         <td><input class="Age" type="text" value="age3"/></td>
         <td><input class="No" type="text" value="no3"/></td>
     </tr>
     ...
</table>

Comments

0

First replace input id's with classes of the same name. Then loop over rows like this:

var arr = [];
$('#table tr').each(function(tr, index) {
  var name = $(tr).find('.Name').val()
  var age = $(tr).find('.Age').val()
  var no = $(tr).find('.No').val()
  arr.push({name:name, age:age, no:no});
})

Comments

0

You should fix the issue with the ids before (ids should be unique in the document) then with jquery will be as easy as:

const inputs = $('#tablo').find('input');

You even don't need jquery

const inputs = document.querySelectorAll('#tablo input');

Comments

0

Using dots in find for read ALL imputs, Like if work for u

$('#bodyt tr').each(function(){
            var inputs = $(this).find(':input'), row={};
            inputs.each(function(){
              row[$(this).attr('idm')]  = this.value;
            });
            ob.solicitud.push(row);
          });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.