Carol Kariuki
Carol Kariuki

Reputation: 115

Get selected value of dropdown

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?

Upvotes: 6

Views: 13760

Answers (7)

Zoren Konte
Zoren Konte

Reputation: 366

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;

Upvotes: 1

Razia sultana
Razia sultana

Reputation: 2211

<select id="ddlViewBy">
  <option value="1">test1</option>
  <option value="2" selected="selected">test2</option>
  <option value="3">test3</option>
</select>

Upvotes: 1

Alessandro
Alessandro

Reputation: 4472

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.

Upvotes: 7

Theo Itzaris
Theo Itzaris

Reputation: 4681

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;

Upvotes: 3

synthet1c
synthet1c

Reputation: 6282

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>

Upvotes: 2

ITFlyer
ITFlyer

Reputation: 182

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;

Upvotes: 4

Stormhashe
Stormhashe

Reputation: 704

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

Upvotes: 1

Related Questions