0

Possible Duplicate:
How to get the selected value of dropdownlist using JavaScript?
javascript selected value

I have a drop down list for quantities, like this:

<select id="Quantity" name="Quantity" class="quantity_select">
    <option id="1" selected="selected" value="1">1</option>
    <option id="2" value="2">2</option>
    <option id="3" value="3">3</option>
</select>

I need to use javascript to find the value of the currently selected quantity. When I select e.g. option 2, the selected="selected" doesn't change to the new option. How can I use the DOM to get the current selected quantity?

Thanks

Community
  • 1
  • 1
user208709
  • 415
  • 1
  • 7
  • 12
  • please share if you have tried some code before posting this Question. There are plenty of questions on SO itself for this logic – Riju Mahna Jan 23 '13 at 12:40

3 Answers3

5

Option 1

HTML

<select id="Quantity" name="Quantity" 
        class="quantity_select" onchange="SetValue(this.value)>
    <option id="1" selected="selected" value="1">1</option>
    <option id="2" value="2">2</option>
    <option id="3" value="3">3</option>
</select>

JavaScript

function SetValue(val) {
    alert(val); // This will be the selected value in the drop down
}

Option 2

If you already have JS in place, and don't want to use option 1, you can simply get the value with getElementById():

var ddl = document.getElementById('Quantity');
var val = ddl.options[ddl.selectedIndex].value;
James Hill
  • 60,353
  • 20
  • 145
  • 161
2

Use document.getElementById('Quantity').value

techfoobar
  • 65,616
  • 14
  • 114
  • 135
2

USe

document.getElementById('Quantity').options[document.getElementById('Quantity').selectedIndex].value;
Dnyan Waychal
  • 1,418
  • 11
  • 27