ashwin
ashwin

Reputation: 99

How to get the selected value of <option> from table and store into array?

var dataList=[];
$("#saveBtn").click(function(e) {
       
    var allOptionsSelected=getEditableTableOptionData("#tempTable");
    console.log(allOptionsSelected);

});
function getEditableTableOptionData(param_table) {
    var tRowList = $(param_table).find("tbody").find("tr");
      
    var dataList = [];
    for (var rIndex = 0; rIndex < tRowList.length; rIndex++) {
        var tRow = tRowList[rIndex];
      
      	var rData = {};
      	var tCellList = $(tRow).find("td");
      	for (var cIndex = 0; cIndex < tCellList.length; cIndex++) {
      		var tCell = tCellList[cIndex];
      		if ($(tCell).find("select").length)
      			rData[$(tCell).find("option").prop("value")] = $(tCell).find(
      						"option").val();
      	}
      	dataList.push(rData);
    }
    return dataList;
}
<!DOCTYPE html>
<html>
   <head>
      <!-- Latest compiled and minified CSS -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
      <!-- jQuery library -->
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <!-- Popper JS -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
      <!-- Latest compiled JavaScript -->
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
      <title></title>
   </head>
   <body>
      <table class="table table-bordered" id="tempTable">
         <thead>
            <tr>
               <th>Type</th>
            </tr>
         </thead>
         <tbody>
            <tr>
               <td>
                  <select>
                     <option value="1">भन्सार महसुल</option>
                     <option value="2">मू.अ.कर</option>
                     <option value="3">अन्त: शुल्क</option>
                     <option value="4">अन्य </option>
                  </select>
               </td>
            </tr>
            <tr>
               <td>
                  <select>
                     <option value="1">भन्सार महसुल</option>
                     <option value="2">मू.अ.कर</option>
                     <option value="3">अन्त: शुल्क</option>
                     <option value="4">अन्य </option>
                  </select>
               </td>
            </tr>
             <tr>
               <td>
                  <select>
                     <option value="1">भन्सार महसुल</option>
                     <option value="2">मू.अ.कर</option>
                     <option value="3">अन्त: शुल्क</option>
                     <option value="4">अन्य </option>
                  </select>
               </td>
            </tr>
         </tbody>
      </table>
      <button id="saveBtn" class="btn btn-primary pull-right">Save</button>
   </body>
   
</html>

I have select Options in a table which is dynamic(I just showed three here).I tried to get the selected value of option and push into array when i click on save button.But when I change the value of select option and click on save button then it is only showing value "1" in when i try to grab the selected value using console.log(allOptionsSelected); .

Though i change the value of these three options it is only showing me "1".It should have been 2 or 3 or 4 according to options selected.

enter image description here

Upvotes: 0

Views: 122

Answers (3)

Pawan
Pawan

Reputation: 31

You can just use below statement for get all selected option values

var dataList=[];
$("#saveBtn").click(function(e) {       
    $("#tempTable tbody tr select option:selected").each(function(){
        dataList.push($(this).val())
    })
    console.log(dataList);
});

Upvotes: 1

brk
brk

Reputation: 50291

The code to get the selected option is bit lengthy. I think that can be shorten.

In the click event handler get all select and iterate it. Then use the val method , to get the selected option.

Also not inside the handler the dataList is emptied to remove the previously selected values

var dataList = [];
$("#saveBtn").click(function(e) {
  dataList.length = 0;
  $("#tempTable").find('select').each((i, v) => {
    dataList.push($(v).val())

  });
  console.log(dataList)

})
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<table class="table table-bordered" id="tempTable">
  <thead>
    <tr>
      <th>Type</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <select>
          <option value="1">भन्सार महसुल</option>
          <option value="2">मू.अ.कर</option>
          <option value="3">अन्त: शुल्क</option>
          <option value="4">अन्य </option>
        </select>
      </td>
    </tr>
    <tr>
      <td>
        <select>
          <option value="1">भन्सार महसुल</option>
          <option value="2">मू.अ.कर</option>
          <option value="3">अन्त: शुल्क</option>
          <option value="4">अन्य </option>
        </select>
      </td>
    </tr>
    <tr>
      <td>
        <select>
          <option value="1">भन्सार महसुल</option>
          <option value="2">मू.अ.कर</option>
          <option value="3">अन्त: शुल्क</option>
          <option value="4">अन्य </option>
        </select>
      </td>
    </tr>
  </tbody>
</table>
<button id="saveBtn" class="btn btn-primary pull-right">Save</button>

Upvotes: 0

ellipsis
ellipsis

Reputation: 12152

find(option) will return the first option element it finds so it is always giving one as output. Use find(option:selected) to get the selected option

var dataList = [];
$("#saveBtn").click(function(e) {

  var allOptionsSelected = getEditableTableOptionData("#tempTable");
  console.log(allOptionsSelected);
});

function getEditableTableOptionData(param_table) {
  var tRowList = $(param_table).find("tbody").find("tr");
  var dataList = [];
  for (var rIndex = 0; rIndex < tRowList.length; rIndex++) {
    var tRow = tRowList[rIndex];
    var rData = {};
    var tCellList = $(tRow).find("td");
    for (var cIndex = 0; cIndex < tCellList.length; cIndex++) {
      var tCell = tCellList[cIndex];
      if ($(tCell).find("select").length)
        rData[$(tCell).find("option").prop("value")] = $(tCell).find(
          "option:selected").val();
    }
    dataList.push(rData);
  }
  return dataList.flatMap(e=>Object.values(e));
}
<!DOCTYPE html>
<html>

<head>
  <!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <!-- jQuery library -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <!-- Popper JS -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <!-- Latest compiled JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
  <title></title>
</head>

<body>
  <table class="table table-bordered" id="tempTable">
    <thead>
      <tr>
        <th>Type</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <select>
            <option value="1">भन्सार महसुल</option>
            <option value="2">मू.अ.कर</option>
            <option value="3">अन्त: शुल्क</option>
            <option value="4">अन्य </option>
          </select>
        </td>
      </tr>
      <tr>
        <td>
          <select>
            <option value="1">भन्सार महसुल</option>
            <option value="2">मू.अ.कर</option>
            <option value="3">अन्त: शुल्क</option>
            <option value="4">अन्य </option>
          </select>
        </td>
      </tr>
      <tr>
        <td>
          <select>
            <option value="1">भन्सार महसुल</option>
            <option value="2">मू.अ.कर</option>
            <option value="3">अन्त: शुल्क</option>
            <option value="4">अन्य </option>
          </select>
        </td>
      </tr>
    </tbody>
  </table>
  <button id="saveBtn" class="btn btn-primary pull-right">Save</button>
</body>

</html>

Upvotes: 1

Related Questions