Select options كوليشن
تاكرى و فالو
options
كوليشن تاكاللان سارى لى ليستو سكيلي ئىپتيئون collection.
Note:The elements in the collection are sorted in the order they appear in the source code.
Instance
Example 1
View how many options are in a specific dropdown list:
var x = document.getElementById("mySelect").options.length;
X kaiwa za yi jayayya:
4
Tip:More examples are provided below the page.
Syntax
selectObject.options
Property
Property | Description |
---|---|
length |
Return the number of <option> elements in the collection. Note:This property is read-only |
selectedIndex | Set or return the index of the selected <option> element in the collection (starting from 0). |
Method
Method | Description |
---|---|
[index] |
Return <option> element with specified index (starting from 0) from the collection. Note:If the index number is out of range, return null. |
[add(option[,index)] | Add <option> element to the specified index in the collection. If the index is not specified, it will be inserted at the end of the collection. |
item(index) |
Return <option> element with specified index (starting from 0) from the collection. Note:If the index number is out of range, return null. |
namedItem(id) |
Return <option> element with specified ID from the collection. Note:If id If not found, return null. |
remove(index) | Remove <option> element with specified index from the collection. |
Technical details
DOM version: | Core Level 2 Document Object |
---|---|
Return value: |
HTMLOptionsCollection object, wanda ke wakilta kaiyarin na all <option> na <select> element. Kaiyarin na kowa suna kaiyewa dake a kaiyewa a kowa a kaninde kudi a kudiya. |
More shirin
Shirin 2: [index]
Gani koyo na farko a kaninde tsakiya 0:
var x = document.getElementById("mySelect").options[0].text;
X kaiwa za yi jayayya:
Apple
Shirin 3: item(index)
Gani koyo na farko a kaninde tsakiya 0:
var x = document.getElementById("mySelect").options.item(0).text;
X kaiwa za yi jayayya:
Apple
Shirin 4: namedItem(id)
Gani koyo 'orange' a kaninde id na tsakiya:
var x = document.getElementById("mySelect").options.namedItem("orange").text;
X kaiwa za yi jayayya:
Orange
Shirin 5
A sakiya 'Kiwi' a kaninde “1” na tsakiya:
var x = document.getElementById("mySelect"); var c = document.createElement("option"); c.text = "Kiwi"; x.options.add(c, 1);
Example 6
Gudan ɗaya na ɗauka da ɗauka ɗaya '1':
var x = document.getElementById("mySelect"); x.options.remove(1);Example 7
Gwajiyar dace sabuwar ɗaya kundin dama, kuma sabuwar alama za a ɗauka a ɗaya na ɗauka:
var x = document.getElementById("mySelect"); var txt = ""; var i; for (i = 0; i < x.length; i++) { txt = txt + x.options[i].text + "<br>"; }
X kaiwa za yi jayayya:
Apple Orange Pineapple Banana
Example 8
Zaɓi wani alama daga ɗaya kundin dama, kuma sabuwar alama za a ɗauka a ɗaya na ɗauka:
var x = document.getElementById("mySelect"); var i = x.selectedIndex; document.getElementById("demo").innerHTML = x.options[i].text;
X kaiwa za yi jayayya:
Banana
Example 9
Duba ɗaya na kundin dama don saɓu alama a ɗaya kundin dama don saɓu alama:
var carsAndModels = {}; carsAndModels['VO'] = ['V70', 'XC60', 'XC90']; carsAndModels['VW'] = ['Golf', 'Polo', 'Scirocco', 'Touareg']; carsAndModels['BMW'] = ['M6', 'X5', 'Z3']; function ChangeCarList() { var carList = document.getElementById("car"); var modelList = document.getElementById("carmodel"); var selCar = carList.options[carList.selectedIndex].value; while (modelList.options.length) { modelList.remove(0); } var cars = carsAndModels[selCar]; if (cars) { var i; for (i = 0; i < cars.length; i++) { var car = new Option(cars[i], i); modelList.options.add(car); } } }