How to check if an item is selected from an HTML drop down list?


I have a drop drown list and I am having trouble checking whether or not a value has been selected from the drop down list

Below is my HTML Code :

<label class="paylabel" for="cardtype">Card Type:</label>
<select id="cardtype" name="cards">
    <option value="selectcard">--- Please select ---</option>
    <option value="mastercard">Mastercard</option>
    <option value="maestro">Maestro</option>
    <option value="solo">Solo (UK only)</option>
    <option value="visaelectron">Visa Electron</option>
    <option value="visadebit">Visa Debit</option>

Below is my JavaScript Code :

var card = document.getElementByName("cards")[0].value;
if (card.value == selectcard) {
    alert("Please select a card type");

This question is tagged with javascript html drop-down-menu html-select

~ Asked on 2013-04-13 11:11:25

The Best Answer is


Well you missed quotation mark around your string selectcard it should be "selectcard"

if (card.value == selectcard)

should be

if (card.value == "selectcard")

Here is complete code for that

function validate()
 var ddl = document.getElementById("cardtype");
 var selectedValue = ddl.options[ddl.selectedIndex].value;
    if (selectedValue == "selectcard")
    alert("Please select a card type");

JS Fiddle Demo

~ Answered on 2013-04-13 11:14:22


var card = document.getElementById("cardtype");
if(card.selectedIndex == 0) {
     alert('select one answer');
else {
    var selectedText = card.options[card.selectedIndex].text;

~ Answered on 2013-04-13 11:38:20

Most Viewed Questions: