Reputation: 535
I have table in my view in which the first column has the Drop down box for each row. The last column has a button, on click of which I need to get the value from the Drop Down box. I have specified ID to each DropDown box. But I am unable to get the correct selected value. If I use a single Drop Down box, I get desired value
HTML Code:
<td class="col-md-2">
@Html.DropDownListFor(model => model.SelectedBranch, Model.BranchList, new { id = "ddBranch-" + item.ID, style = "display:none;", @Class = "form-control m-right-sm" })
</td>
JS Code:
If I use only one dropdown box, below code works fine:
function update(id) {
var branchid = $("#SelectedBranch").val();
But since I have multiple Dropdown box, I tried using below code:
function update(id) {
var e = document.getElementById("ddBranch-" + id);
var br = e.options[e.selectedIndex].value;
Now the first statement works fine. I get values in var e
. I checked and found that the second item has property selected
to true
. But the second statement gives error. I tried couple of similar approach suggested on SO but nothing seem to be working. I am using asp.net mvc .Please advise.
Upvotes: 1
Views: 792
Reputation: 19007
You are just doing it the hard way, Since you already have dynamic id's to each drop-down you can build the same id by string concatenation. Use this
var branchid = $("#ddBranch-" + id).val();
Upvotes: 2