L'interface HTMLSelectElement
représente un élément HTML <select>
. Cet élément partage aussi toute les propriétés et méthodes des autres elements HTML via l'interface HTMLElement
.
Propriétés
Cette interface hérite des propriétés de HTMLElement
, Element
et Node
.
HTMLSelectElement.autofocus
- Est un
Boolean
qui reflète l'attribut HTMLautofocus
: il indique si l'élément a le focus au chargement de la page (excepté si l'utilisateur le change, par exemple en cliquant sur un autre element). Uniquement les elément associé à un formulaire dans le document peuvent utiliser cet attribut.
HTMLSelectElement.disabled
- Est un
Boolean
qui reflète l'attribut HTMLdisabled
: il indique si l'élément est oui ou non desactivé. Si il est désactivé, l'élément n'accepte pas les clicks. HTMLSelectElement.form
Lecture seule- Retourne un
HTMLFormElement
représentant le formulaire associé a cet élément. Si cet élément est le fils d'un formulaire, alors cet attribut est l'ID de l'element form. Si l'élément n'est pas fils d'un formulaire, alors l'attribut peut-être l'ID de n'importe quel élément du même document. HTMLSelectElement.labels
Lecture seule- Retourne une
NodeList
contenant la liste des labels associé avec l'éléments select. HTMLSelectElement.length
- Est un
unsigned long
représentant le nombre d'élément<option>
dans cet elementselect
. HTMLSelectElement.multiple
- Est un
Boolean
représentant l'attribut HTMLmultiple
, qui indique si plusieurs items peuvent être choisis à la fois par l'utilisateur. HTMLSelectElement.name
- Est une
DOMString
qui représente l'attribut HTMLname
, contenant le nom de ce contrôle pour les serveurs et pour les fonctions de recherche dans le DOM. HTMLSelectElement.options
Lecture seule- Returns a
HTMLOptionsCollection
containing the set of<option>
elements contained by this element. HTMLSelectElement.required
- Is a
Boolean
that reflects therequired
HTML attribute, which indicates whether the user is required to select a value before submitting the form. HTMLSelectElement.selectedIndex
- Is a
long
that reflects the index of the first selected<option>
element. The value-1
indicates no element is selected. HTMLSelectElement.selectedOptions
Lecture seule- Returns a live
HTMLCollection
containing the set of options that are selected. HTMLSelectElement.size
- Is a
long
that reflects thesize
HTML attribute, which contains the number of visible items in the control. The default is 1, unlessmultiple
is true, in which case it is 4. HTMLSelectElement.type
Lecture seule- Returns a
DOMString
the form control's type. Whenmultiple
istrue
, it returns"select-multiple"
; otherwise, it returns"select-one"
. HTMLSelectElement.validationMessage
Lecture seule- Returns a
DOMString
containing a localized message that describes the validation constraints that the control does not satisfy (if any). This attribute is the empty string if the control is not a candidate for constraint validation (willValidate
is false), or it satisfies its constraints. HTMLSelectElement.validity
Lecture seule- Returns a
ValidityState
representing the validity state that this control is in. HTMLSelectElement.value
- Is a
DOMString
with the value of this form control, that is, of the first selected option. HTMLSelectElement.willValidate
Lecture seule- Is a
Boolean
that indicates whether the button is a candidate for constraint validation. It is false if any conditions bar it from constraint validation.
Méthodes
This interface inherits the methods of HTMLElement
, and of Element
and Node
.
HTMLSelectElement.add()
- Adds an element to the collection of
option
elements for thisselect
element. HTMLSelectElement.blur()
- Removes input focus from this element. This method is now implemented on
HTMLElement
. HTMLSelectElement.checkValidity()
- Checks whether the element has any constraints and whether it satisfies them. If the element fails its constraints, the browser fires a cancelable
invalid
event at the element (and returnsfalse
). HTMLSelectElement.focus()
- Gives input focus to this element. This method is now implemented on
HTMLElement
. HTMLSelectElement.item()
- Gets an item from the options collection for this
<select>
element. You can also access an item by specifying the index in array-style brackets or parentheses, without calling this method explicitly. HTMLSelectElement.namedItem()
- Gets the item in the options collection with the specified name. The name string can match either the
id
or thename
attribute of an option node. You can also access an item by specifying the name in array-style brackets or parentheses, without calling this method explicitly. HTMLSelectElement.remove()
- Removes the element at the specified index from the options collection for this select element.
HTMLSelectElement.setCustomValidity()
- Sets the custom validity message for the selection element to the specified message. Use the empty string to indicate that the element does not have a custom validity error.
Exemple
Get information about the selected option
/* assuming we have the following HTML
<select id='s'>
<option>First</option>
<option selected>Second</option>
<option>Third</option>
</select>
*/
var select = document.getElementById('s');
// return the index of the selected option
alert(select.selectedIndex); // 1
// return the value of the selected option
alert(select.options[select.selectedIndex].value) // Second
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard La définition de 'HTMLSelectElement' dans cette spécification. |
Standard évolutif | Since the latest snapshot, HTML5, it adds the autocomplete property and the reportValidity() method. |
HTML5 La définition de 'HTMLSelectElement' dans cette spécification. |
Recommendation | Is a snapshot of HTML Living Standard. It adds the autofocus , form , required , labels , selectedOptions , willValidate , validity and validationMessage properties.The tabindex property and the blur() and focus() methods have been moved to HTMLElement .The methods item() , namedItem() , checkValidity() and setCustomValidity() . |
Document Object Model (DOM) Level 2 HTML Specification La définition de 'HTMLSelectElement' dans cette spécification. |
Obsolete | options now returns an HTMLOptionsCollection .length now returns an unsigned long . |
Document Object Model (DOM) Level 1 Specification La définition de 'HTMLSelectElement' dans cette spécification. |
Obsolete | Initial definition. |
Browser compatibility
Nous convertissons les données de compatibilité dans un format JSON.
Ce tableau de compatibilité utilise encore l'ancien format
car nous n'avons pas encore converti les données qu'il contient.
Vous pouvez nous aider en contribuant !
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 1.0 | 1.0 (1.7 ou moins) | 1.0 | 1.0 | 1.0 |
item() and namedItem() |
(Oui) | 4.0 (2.0) | ? | (Oui) | (Oui) |
setCustomValidity() , checkValidity() , willValidate , validationMessage , validity |
(Oui) | 4.0 (2.0) | ? | (Oui) | ? |
selectedOptions |
(Oui) | 26 (26) | ? | (Oui) | (Oui) |
labels |
(Oui) | Non implémenté (voir bug 556743) | ? | (Oui) | (Oui) |
Feature | Android | Chrome | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | 1.0 | 1.0 | 1.0 (1) | 1.0 | 1.0 | 1.0 | 1.0 |
item() and namedItem() |
? | ? | 4.0 (2.0) | 1.0 | ? | ? | (Oui) |
setCustomValidity() , checkValidity() , willValidate , validationMessage , validity |
? | ? | 4.0 (2.0) | 1.0 | ? | ? | ? |
selectedOptions |
? | ? | 26.0 (26) | 1.2 | ? | ? | (Oui) |
labels |
? | ? | Non implémenté (voir bug 556743) | Non implémenté (voir bug 556743) | ? | ? | (Oui) |
See also
- The
<select>
HTML element, implementing this interface.