views:

73

answers:

4

Hi There,

I am having trouble finding out how to figure out if a a html dropdown box has been set?

Ok, I understand how I acn check the index once it has been activate and an item chosen. But, how do I determine that the box ahsn't been touched and no item selected?

Thanks.

A: 

The select.options.selectedIndex will be -1 in that case.

Robusto
On a drop-down select box, an item will always be selected. If no default is set, the selected index will be 0. In a select box with the size or multiple attributes, when no items are selected the index will be -1, but the OP specifically mentioned drop down.
Andy E
@Andy E's head: I understood him to mean "no item selected" ... which is possible, but you're right, not if the box "ahsn't been touched". Self-contradictory statement. In any case, if no item has been selected, the index will be -1. And it can be set to -1 to select nothing.
Robusto
@Robusto: it *can* be set to -1, but it's not set to -1 by default. I knocked up a very quick example for you - http://jsfiddle.net/fYju6/. The page loads and alerts the selectedIndex, which is 0. **edit** nm, I misread your last comment as an argument when it was actually an agreement ;-)
Andy E
My understanding was that the OP wants to know if an element hasn't been changed
David Caunt
Perfect.. thanks.I'm using a multiline select box.Why didn't I just print the selectedIndex property when there was nothing selected, D'oh..Cheers guys.
Conor H
A: 
I don't think there's a native method, but you can add an event listener to call a function when the select is changed:

function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    }
    else if (elm.attachEvent) {
        var r = elm.attachEvent('on' + evType, fn);
        return r;
    }
    else {
        elm['on' + evType] = fn;
    }
}
hasBeenTouched = false;
addEvent(selectEl, 'change', function() {
    hasBeenTouched = true;
});
David Caunt
@David, it would be wiser to use the focus or blur event, as the change event doesn't fire if you select the currently selected option in the list.
Andy E
A: 

After the document is loaded (naively window.onload), manually set the drop down list selectedIndex to -1 (as others have observed, the default is otherwise for the first item to be selected)

window.onload = function() {
    document.getElementById('ddlId').selectedIndex = -1;
};

There's no way for the user to set the drop down list to unselected through the browser interface, so you can be sure it is both unset and "untouched" by testing whether the selectedIndex is still -1 at any point after initially setting it yourself.

Stephen Swensen
A: 

It sounds like you want to make sure a user has selected an option and not just submitted the form with the default option. This situation is commonly achieved by having a "label" option (sometimes blank to keep the size down) in your drop down box, that is selected by default, followed by validation checking to see if another option has been selected:

<select>
  <option>-- Please choose an option -- </option>
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
</select>

Using validation properly (both server-side and client-side), you're able to implement a solution that doesn't rely on Javascript to check if an option has been properly selected.

Andy E