Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have 5 columns in my jQuery data table and the fifth one is a dropdown list. I want to save the data in this data table. So I want to iterate through all the columns of all rows in this data table. The fifth column is a drop down list and the user selects one item from the list. The item's name is displayed in the dropdown list but each dropdown item has an id value and a text name in it. And "None" is the default value shown in the drop down list at first. The Dataset is used to populate the data table and Dropdown list is also populated with another dataset.

In .cshtml
Razor
@foreach (System.Data.DataRow row in DataSet.Tables[0].Rows)
   {
           @foreach (System.Data.DataColumn column in DataSet.Tables[0].Columns)
        {
                    @row[column]
                 }
                 <div class="ddlPanel">
         
         None
         @foreach (var item in ViewBag.DropdownOptions) // viewbag is dataset
          {
           @item.Text
          }
     </div>

And the code for getting each column values of each row including the id and text value of the dropdown is shown below:

What I have tried:

JavaScript
function Save() {

               var newDoorListForAdd = new Array();
               var table = document.getElementById("newTable");
               var tableLength = table.rows.length;
               for (var i = 0; i < tableLength - 1; i++) {            
                    var trial = $("#ddlSettingName option:selected").text();
                    alert(trial);
               }
            }


The above code is not working. I want to get all the values (including id and text value of dropdown selected item) of each row. How can I achieve it ? This application is ASP.Net Core MVC C#.
Posted
Updated 14-Sep-24 6:47am
v2

1 solution

$("#ddlSettingName option:selected") returns the selected option from the element with the ID set to ddlSettingName.

IDs within a document must be unique. If you have multiple elements with the same ID, the browser will ignore it for all but one. Passing the ID to getElementById, or to jQuery's ID selector, will only reference one of the elements.

You need to give each of your lists a different ID, or find another way to identify them. One option would be to give the list a dummy "marker" class - eg: class="--js-ddl-setting-name", combined with $(".--js-ddl-setting-name option:selected", table.rows[i]).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900