Class HtmlSelect

All Implemented Interfaces:
Serializable, Cloneable, DisabledElement, FormFieldWithNameHistory, LabelableElement, SubmittableElement, ValidatableElement, Element, Node

Wrapper for the HTML element "select".
See Also:
  • Field Details

  • Method Details

    • onAllChildrenAddedToPage

      public void onAllChildrenAddedToPage(boolean postponed)
      If we were given an invalid size attribute, normalize it. Then set a default selected option if none was specified and the size is 1 or less and this isn't a multiple selection input.
      Overrides:
      onAllChildrenAddedToPage in class DomNode
      Parameters:
      postponed - whether to use PostponedAction or no
    • handles

      public boolean handles(org.htmlunit.javascript.host.event.Event event)
      Indicates if the provided event can be applied to this node. Overwrite this.
      Overrides:
      handles in class HtmlElement
      Parameters:
      event - the event
      Returns:
      false if the event can't be applied
    • getSelectedOptions

      public List<HtmlOption> getSelectedOptions()

      Returns all of the currently selected options. The following special conditions can occur if the element is in single select mode:

      • if multiple options are erroneously selected, the last one is returned
      • if no options are selected, the first one is returned
      Returns:
      the currently selected options
    • getOptions

      public List<HtmlOption> getOptions()
      Returns all of the options in this select element.
      Returns:
      all of the options in this select element
    • getOption

      public HtmlOption getOption(int index)
      Returns the indexed option.
      Parameters:
      index - the index
      Returns:
      the option specified by the index
    • getOptionSize

      public int getOptionSize()
      Returns the number of options.
      Returns:
      the number of options
    • setOptionSize

      public void setOptionSize(int newLength)
      Remove options by reducing the "length" property. This has no effect if the length is set to the same or greater.
      Parameters:
      newLength - the new length property value
    • removeOption

      public void removeOption(int index)
      Remove an option at the given index.
      Parameters:
      index - the index of the option to remove
    • replaceOption

      public void replaceOption(int index, HtmlOption newOption)
      Replace an option at the given index with a new option.
      Parameters:
      index - the index of the option to remove
      newOption - the new option to replace to indexed option
    • appendOption

      public void appendOption(HtmlOption newOption)
      Add a new option at the end.
      Parameters:
      newOption - the new option to add
    • appendChild

      public DomNode appendChild(Node node)
      Specified by:
      appendChild in interface Node
      Overrides:
      appendChild in class DomNode
    • setSelectedAttribute

      public <P extends Page> P setSelectedAttribute(String optionValue, boolean isSelected)
      Sets the "selected" state of the specified option. If this "select" element is single-select, then calling this method will deselect all other options.

      Only options that are actually in the document may be selected.

      Type Parameters:
      P - the page type
      Parameters:
      optionValue - the value of the option that is to change
      isSelected - true if the option is to become selected
      Returns:
      the page contained in the current window as returned by WebClient.getCurrentWindow()
    • setSelectedAttribute

      public <P extends Page> P setSelectedAttribute(String optionValue, boolean isSelected, boolean invokeOnFocus)
      INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
      Sets the "selected" state of the specified option. If this "select" element is single-select, then calling this method will deselect all other options.

      Only options that are actually in the document may be selected.

      Type Parameters:
      P - the page type
      Parameters:
      optionValue - the value of the option that is to change
      isSelected - true if the option is to become selected
      invokeOnFocus - whether to set focus or not.
      Returns:
      the page contained in the current window as returned by WebClient.getCurrentWindow()
    • setSelectedAttribute

      public <P extends Page> P setSelectedAttribute(HtmlOption selectedOption, boolean isSelected)
      Sets the "selected" state of the specified option. If this "select" element is single-select, then calling this method will deselect all other options.

      Only options that are actually in the document may be selected.

      Type Parameters:
      P - the page type
      Parameters:
      selectedOption - the value of the option that is to change
      isSelected - true if the option is to become selected
      Returns:
      the page contained in the current window as returned by WebClient.getCurrentWindow()
    • setSelectedAttribute

      public <P extends Page> P setSelectedAttribute(HtmlOption selectedOption, boolean isSelected, boolean invokeOnFocus, boolean shiftKey, boolean ctrlKey, boolean isClick)
      INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
      Sets the "selected" state of the specified option. If this "select" element is single-select, then calling this method will deselect all other options.

      Only options that are actually in the document may be selected.

      Type Parameters:
      P - the page type
      Parameters:
      selectedOption - the value of the option that is to change
      isSelected - true if the option is to become selected
      invokeOnFocus - whether to set focus or not.
      shiftKey - true if SHIFT is pressed
      ctrlKey - true if CTRL is pressed
      isClick - is mouse clicked
      Returns:
      the page contained in the current window as returned by WebClient.getCurrentWindow()
    • getSubmitNameValuePairs

      public NameValuePair[] getSubmitNameValuePairs()

      Returns an array of NameValuePairs that are the values that will be sent back to the server whenever this element's containing form is submitted.

      THIS METHOD IS INTENDED FOR THE USE OF THE FRAMEWORK ONLY AND SHOULD NOT BE USED BY CONSUMERS OF HTMLUNIT. USE AT YOUR OWN RISK.

      Specified by:
      getSubmitNameValuePairs in interface SubmittableElement
      Returns:
      the values that will be sent back to the server whenever this element's containing form is submitted
    • reset

      public void reset()
      Returns the value of this element to what it was at the time the page was loaded.
      Specified by:
      reset in interface SubmittableElement
    • setDefaultValue

      public void setDefaultValue(String defaultValue)
      Sets the default value to use when this element gets reset, if applicable.
      Specified by:
      setDefaultValue in interface SubmittableElement
      Parameters:
      defaultValue - the default value to use when this element gets reset, if applicable
      See Also:
    • getDefaultValue

      public String getDefaultValue()
      Returns the default value to use when this element gets reset, if applicable.
      Specified by:
      getDefaultValue in interface SubmittableElement
      Returns:
      the default value to use when this element gets reset, if applicable
      See Also:
    • setDefaultChecked

      public void setDefaultChecked(boolean defaultChecked)
      Sets the default checked state to use when this element gets reset, if applicable. The default implementation is empty; only checkboxes and radio buttons really care what the default checked value is. This implementation is empty; only checkboxes and radio buttons really care what the default checked value is.
      Specified by:
      setDefaultChecked in interface SubmittableElement
      Parameters:
      defaultChecked - the default checked state to use when this element gets reset, if applicable
      See Also:
    • isDefaultChecked

      public boolean isDefaultChecked()
      Returns the default checked state to use when this element gets reset, if applicable. This implementation returns false; only checkboxes and radio buttons really care what the default checked value is.
      Specified by:
      isDefaultChecked in interface SubmittableElement
      Returns:
      the default checked state to use when this element gets reset, if applicable
      See Also:
    • isMultipleSelectEnabled

      public boolean isMultipleSelectEnabled()
      Returns true if this select is using "multiple select".
      Returns:
      true if this select is using "multiple select"
    • getOptionByValue

      public HtmlOption getOptionByValue(String value) throws ElementNotFoundException
      Returns the HtmlOption object that corresponds to the specified value.
      Parameters:
      value - the value to search by
      Returns:
      the HtmlOption object that corresponds to the specified value
      Throws:
      ElementNotFoundException - If a particular element could not be found in the DOM model
    • getOptionByText

      public HtmlOption getOptionByText(String text) throws ElementNotFoundException
      Returns the HtmlOption object that has the specified text.
      Parameters:
      text - the text to search by
      Returns:
      the HtmlOption object that has the specified text
      Throws:
      ElementNotFoundException - If a particular element could not be found in the DOM model
    • getNameAttribute

      public final String getNameAttribute()
      Returns the value of the attribute name. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute name or an empty string if that attribute isn't defined
    • getSizeAttribute

      public final String getSizeAttribute()
      Returns the value of the attribute size. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute size or an empty string if that attribute isn't defined
    • getSize

      public final int getSize()
      Returns:
      the size or 1 if not defined or not convertable to int
    • getMultipleAttribute

      public final String getMultipleAttribute()
      Returns the value of the attribute multiple. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute multiple or an empty string if that attribute isn't defined
    • getDisabledAttribute

      public final String getDisabledAttribute()
      Returns the value of the attribute disabled. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Specified by:
      getDisabledAttribute in interface DisabledElement
      Returns:
      the value of the attribute disabled or an empty string if that attribute isn't defined
    • isDisabled

      public final boolean isDisabled()
      Returns true if the disabled attribute is set for this element.
      Specified by:
      isDisabled in interface DisabledElement
      Returns:
      true if the disabled attribute is set for this element
    • isReadOnly

      public boolean isReadOnly()
      Returns true if this element is read only.
      Returns:
      true if this element is read only
    • getTabIndexAttribute

      public final String getTabIndexAttribute()
      Returns the value of the attribute tabindex. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute tabindex or an empty string if that attribute isn't defined
    • getOnFocusAttribute

      public final String getOnFocusAttribute()
      Returns the value of the attribute onfocus. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute onfocus or an empty string if that attribute isn't defined
    • getOnBlurAttribute

      public final String getOnBlurAttribute()
      Returns the value of the attribute onblur. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute onblur or an empty string if that attribute isn't defined
    • getOnChangeAttribute

      public final String getOnChangeAttribute()
      Returns the value of the attribute onchange. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute onchange or an empty string if that attribute isn't defined
    • setAttributeNS

      protected void setAttributeNS(String namespaceURI, String qualifiedName, String attributeValue, boolean notifyAttributeChangeListeners, boolean notifyMutationObservers)
      Sets the value of the attribute specified by namespace and qualified name.
      Overrides:
      setAttributeNS in class HtmlElement
      Parameters:
      namespaceURI - the URI that identifies an XML namespace
      qualifiedName - the qualified name (prefix:local) of the attribute
      attributeValue - the value of the attribute
      notifyAttributeChangeListeners - to notify the associated HtmlAttributeChangeListeners
      notifyMutationObservers - to notify MutationObservers or not
    • getOriginalName

      public String getOriginalName()
      Gets the first value of the name attribute of this field before any change.
      Specified by:
      getOriginalName in interface FormFieldWithNameHistory
      Returns:
      the original name (which is the same as the current one when no change has been made)
    • getNewNames

      public Collection<String> getNewNames()
      Get all the names this field got after the original one.
      Specified by:
      getNewNames in interface FormFieldWithNameHistory
      Returns:
      an empty collection if the name attribute has never been changed.
    • getDefaultStyleDisplay

      public HtmlElement.DisplayStyle getDefaultStyleDisplay()
      INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
      Returns the default display style.
      Overrides:
      getDefaultStyleDisplay in class HtmlElement
      Returns:
      the default display style
    • getSelectedIndex

      public int getSelectedIndex()
      Returns the value of the selectedIndex property.
      Returns:
      the selectedIndex property
    • setSelectedIndex

      public void setSelectedIndex(int index)
      Sets the value of the selectedIndex property.
      Parameters:
      index - the new value
    • ensureSelectedIndex

      public void ensureSelectedIndex()
      INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
      Resets the selectedIndex if needed.
    • indexOf

      public int indexOf(HtmlOption option)
      INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
      Parameters:
      option - the option to search for
      Returns:
      the index of the provided option or zero if not found
    • isRequiredSupported

      protected boolean isRequiredSupported()
      Returns whether this element supports the required constraint.
      Overrides:
      isRequiredSupported in class HtmlElement
      Returns:
      whether this element supports the required constraint
    • willValidate

      public boolean willValidate()
      Specified by:
      willValidate in interface ValidatableElement
      Returns:
      whether the element is a candidate for constraint validation
    • setCustomValidity

      public void setCustomValidity(String message)
      Sets the custom validity message for the element to the specified message.
      Specified by:
      setCustomValidity in interface ValidatableElement
      Parameters:
      message - the new message
    • isValid

      public boolean isValid()
      Returns whether this element satisfies all form validation constraints set.
      Overrides:
      isValid in class HtmlElement
      Returns:
      whether this element satisfies all form validation constraints set
    • isCustomErrorValidityState

      public boolean isCustomErrorValidityState()
      Specified by:
      isCustomErrorValidityState in interface ValidatableElement
      Returns:
      a boolean value indicating whether the element's custom validity message has been set to a non-empty string by calling the element's setCustomValidity() method.
    • isValidValidityState

      public boolean isValidValidityState()
      Specified by:
      isValidValidityState in interface ValidatableElement
      Returns:
      true if the element meets all its validation constraints, and is therefore considered to be valid, or false if it fails any constraint. If true, the element matches the :valid CSS pseudo-class; the :invalid CSS pseudo-class otherwise.
    • isValueMissingValidityState

      public boolean isValueMissingValidityState()
      Specified by:
      isValueMissingValidityState in interface ValidatableElement
      Returns:
      true if the element has a required attribute, but no value, or false otherwise. If true, the element matches the :invalid CSS pseudo-class.