public int getSelectedIndex() { NodeList options; int i; // Use getElementsByTagName() which creates a snapshot of all the // OPTION elements under this SELECT. Access to the returned NodeList // is very fast and the snapshot solves many synchronization problems. // Locate the first selected OPTION and return its index. Note that // the OPTION might be under an OPTGROUP. options = getElementsByTagName( "OPTION" ); for ( i = 0 ; i < options.getLength() ; ++i ) if ( ( (HTMLOptionElement) options.item( i ) ).getSelected() ) return i; return -1; }
public int getSelectedIndex() { HTMLCollection options = getOptions(); for (int i = 0; i < options.getLength(); i++) { if (((HTMLOptionElement)options.item(i)).getSelected()) return i; } return isMultiSelect() ? -1 : 0; }
public String getValue() { HTMLCollection options = getOptions(); for (int i = 0; i < options.getLength(); i++) { HTMLOptionElement optionElement = ((HTMLOptionElement)options.item(i)); if (optionElement.getSelected()) return optionElement.getValue(); } return (isMultiSelect() || options.getLength() == 0) ? null : ((HTMLOptionElement)options.item(0)).getValue(); }
@Override public Optional<String> getValue() { String value = null; if (element instanceof HTMLInputElement) { value = ((HTMLInputElement) element).getValue(); } else if (element instanceof HTMLOptionElement) { value = ((HTMLOptionElement) element).getValue(); } return value == null || value.isEmpty() ? Optional.empty() : Optional.of(value); }
@Override public Element setValue(String value) { if (element instanceof HTMLInputElement) { ((HTMLInputElement) element).setValue(value); } else if (element instanceof HTMLOptionElement) { ((HTMLOptionElement) element).setValue(value); } return this; }
@Override public Optional<Option> getOption() { if (element instanceof HTMLOptionElement) { return Optional.of(new Option(this)); } else { return Optional.empty(); } }