3

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
  • 11,496
  • 11
  • 63
  • 74
Carol Kariuki
  • 85
  • 1
  • 8

7 Answers7

4

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
  • 174
  • 1
  • 2
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,224
  • 5
  • 30
  • 65
1

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

Stormhashe
  • 704
  • 6
  • 16
1

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
  • 5,855
  • 2
  • 19
  • 34
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
  • 3,843
  • 3
  • 31
  • 57
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,051
  • 3
  • 13
  • 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
  • 224
  • 5
  • 8