4

I'm having a rather different structure of HTML and I am trying to get the selected value of a dropdown list as seen in the below code:

<div class="quantityDropdown quantityDropdown-cartItem">
    <select id="qtySelect-61224b70-7b26-11e6-91d5-6921d6fe7421" class="cart-quantity-picker"
            data-orderitem="61224b70-7b26-11e6-91d5-6921d6fe7421">
        <option value="1">1</option>
        <option value="2" selected="">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
    </select>
</div>

My code returns null.

I want to get 2 as my value. As you can see, there's no value in the selected attribute.

var quantity = document.querySelector(".quantityDropdown select").getAttribute("selected");

How do I get the value 2 that's outside the option tag?

DimaSan
  • 12,264
  • 11
  • 65
  • 75
Carol Kariuki
  • 95
  • 1
  • 1
  • 8

7 Answers7

4

You should get the option tag, like this:

var quantity = document.querySelector(".quantityDropdown select option:checked");

You could use checked also for checkbox and radio.

Alessandro
  • 4,382
  • 8
  • 36
  • 70
3

This will give you the value of the selected item:

quantity = document.querySelector(".quantityDropdown select").value;

However if you want the actual content of the selected item (i.e. the text shown) outside of the option tag, use:

var quantity = document.querySelector(".quantityDropdown select")[document.querySelector(".quantityDropdown select").selectedIndex].innerHTML;
ITFlyer
  • 172
  • 1
  • 2
2

To get the selected option value you need to use two properties on the select element: options and selectedIndex to get the html option element, then you can get the value.

const dropdown = document.querySelector('.cart-quantity-picker')

console.log('selectedIndex', dropdown.selectedIndex)
console.log('options', dropdown.options)
console.log('selected', dropdown.options[dropdown.selectedIndex].value)
<div class="quantityDropdown quantityDropdown-cartItem">
  <select id="qtySelect-61224b70-7b26-11e6-91d5-6921d6fe7421" class="cart-quantity-picker" data-orderitem="61224b70-7b26-11e6-91d5-6921d6fe7421">
    <option value="1">1</option>
    <option value="2" selected>2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </select>
</div>
synthet1c
  • 6,152
  • 2
  • 24
  • 39
1

Use document.querySelector(".quantityDropdown select").selectedIndex

Stormhashe
  • 704
  • 6
  • 16
1

in case you need the get the 'value' :

var dropdown = document.getElementById("qtySelect-61224b70-7b26-11e6-91d5-6921d6fe7421");
var selValue = dropdown.options[dropdown.selectedIndex].value;

But if you want to get the text ΝΟΤ the value , you do:

 var dropdown = document.getElementById("qtySelect-61224b70-7b26-11e6-91d5-6921d6fe7421");
    var selText = dropdown.options[dropdown.selectedIndex].text;
Theo Itzaris
  • 4,321
  • 3
  • 37
  • 68
1
<select id="ddlViewBy">
  <option value="1">test1</option>
  <option value="2" selected="selected">test2</option>
  <option value="3">test3</option>
</select>
Razia sultana
  • 2,168
  • 3
  • 15
  • 20
1

For getting the value you can use

document.querySelector('#qtySelect option:checked').value;

For getting the actual text inside the option you can use

document.querySelector('#qtySelect option:checked').text;
Zoren Konte
  • 306
  • 4
  • 12