By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

Getting drop-down select value for each row

P: 1
Given http://codepen.io/caim/pen/pggZOx

I'm trying to replace the first column from input to select dropdown but having trouble figuring out how to read the data for each cell.

It was working fine with an input and normal td but now the select entered the picture and it's skipping one column.

The day drop-downs are for newly added rows currently.

This is how I'm trying to read the data

Expand|Select|Wrap|Line Numbers
  1. $BTN.click(function () {
  2.   var $rows = $TABLE.find('tr:not(:hidden)');
  3.   var $tds = $TABLE.find('td:not(:hidden)');
  4.   var headers = [];
  5.   var data = [];
  6.  
  7.  
  8.   // Get the headers (add special header logic here)
  9.   $($rows.shift()).find('th:not(:empty)').each(function () {
  10.     headers.push($(this).text().toLowerCase());
  11.   });
  12.  
  13.   // Turn all existing rows into a loopable array
  14.  
  15.   $rows.each(function () {
  16.     var $td = $(this).find('td');
  17.     var $td2 = $td.find('input'); 
  18.     var $td3 = $td.find('select');
  19.     var h2 = {};  
  20.     // Use the headers from earlier to name our hash keys
  21.     headers.forEach(function (header, i) {
  22.     // h2[header] = $td.eq(i).text();  
  23.  
  24.       //the dropdown value -> 
  25.     //console.log($td3.eq(i).val());
  26.       h2[header] = $td3.eq(i).val();
  27.       if ($td3.eq(i).val() === undefined){ // it's not dropdown ? read it normaly.. 
  28.         console.log(i);        
  29.         h2[header] = ($td.eq(i).text() == '') ? $td2.eq(i).val() : $td.eq(i).text();
  30.       }                 
  31.       //console.log(h2[header]);  
  32.  
  33.     });   
  34.     data.push(h2);
  35.   });
  36.  
  37.   // Output the result
  38.   $EXPORT.text(JSON.stringify(data));
  39. });
Jan 19 '16 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.