0

I am stuck & want to get selected the select option.

<select class="form-control" data-live-search="true" id="itemb">
<option data-tokens="All">All</option>

@foreach (var item in ViewBag.brandCategories)
{                                    
  <option>@item.brandName</option>
}
</select>

//search by brand name..
    $("#itemb").change(function () {
        
        var typp = document.getElementById("typ").value;
        var type = encodeURIComponent(typp);
        var brand = this.value;
        window.location.href = '/mobiles_tablets/item/?type=' + type + '&search=' + brand;
    });

In the above code just i get list in @foreach through @viewBag .but now I want when it return to the view it should get selected value.

please write js code?

Thanks.

  • Possible duplicate of [Html.DropdownListFor selected value not being set](https://stackoverflow.com/questions/19476530/html-dropdownlistfor-selected-value-not-being-set) – GSerg Dec 27 '17 at 07:46
  • can you provide the required answer? – Tahir Zaman Dec 27 '17 at 11:17

2 Answers2

1

This snippet of code will help you to get the selected value of select element. You can modify/change it through codepen check out the below link.

https://codepen.io/uicreation/pen/KZmXbw

var el = document.getElementById('yourId');
el.onchange = function(){
  console.log(this.selectedOptions[0].value);
}
<select id="yourId">
  <option>one</option>
  <option>two</option>
  <option>three</option>
  <option>four</option>
  <option>five</option>
</select>
  • thanks for the reply, but its not worked i did as below but not working... $(document).ready(function () { var el = document.getElementById('itemb'); el.onchange = function () { console.log(this.selectedOptions[0].value); } }); – Tahir Zaman Dec 31 '17 at 14:57
  • i updated the question and show you the js code that onchange what i am searching which works fine, but when it get return to the view then in drobdown the searched value is not selected, please help me thanks – Tahir Zaman Dec 31 '17 at 15:05
  • Hi, as i saw you want to access this.value on another location that mean after executing "window.location.href" page will reload so the "var brand" is lose his value. To know more hope this article will help you https://stackoverflow.com/questions/28317182/how-to-pass-a-value-to-razor-variable-from-javascript-variable – Shivam Kumar Jan 01 '18 at 07:47
  • actually the scenario is that i have the view named "item.chtml" in which i have it goes to action and return to the same "item.chtml" view which working fine but the so please help me. – Tahir Zaman Jan 01 '18 at 11:30
0

or if you want to get selected option index use this.

var el = document.getElementById('yourId');
el.onchange = function(){
  console.log(this.selectedIndex);
}
  • thanks for the reply, but its not worked i did as below but not working... $(document).ready(function () { var el = document.getElementById('itemb'); el.onchange = function () { console.log(this.selectedIndex); } }); – Tahir Zaman Dec 31 '17 at 14:58
  • i updated the question and show you the js code that onchange what i am searching which works fine, but when it get return to the view then in drobdown the searched value is not selected, please help me thanks – Tahir Zaman Dec 31 '17 at 15:05