विकल्प चुनें संग्रह
उदाहरण
पता लगाएँ कि एक विशिष्ट ड्रॉप-डाउन सूची में कितने विकल्प हैं:
var x = document.getElementById("mySelect").options.length;
एक्स का परिणाम होगा:
4
नीचे और अधिक "इसे स्वयं आज़माएं" उदाहरण।
परिभाषा और उपयोग
विकल्प संग्रह ड्रॉप-डाउन सूची में सभी <विकल्प> तत्वों का संग्रह देता है।
नोट: संग्रह में तत्वों को सॉर्ट किया जाता है जैसे वे स्रोत कोड में दिखाई देते हैं।
ब्राउज़र समर्थन
Collection | |||||
---|---|---|---|---|---|
options | Yes | Yes | Yes | Yes | Yes |
वाक्य - विन्यास
selectObject.options
गुण
Property | Description |
---|---|
length | Returns the number of <option> elements in the collection. Note: This property is read-only |
selectedIndex | Sets or returns the index of the selected <option> element in the collection (starts at 0) |
तरीकों
Method | Description |
---|---|
[index] | Returns the <option> element from the collection with the specified index (starts at 0). Note: Returns null if the index number is out of range |
[add(option[,index])] | Adds an <option> element into the collection at the specified index. If no index is specified, it inserts the option at the end of the collection |
item(index) | Returns the <option> element from the collection with the specified index (starts at 0). Note: Returns null if the index number is out of range |
namedItem(id) | Returns the <option> element from the collection with the specified id. Note: Returns null if the id does not exist |
remove(index) | Removes the <option> element with the specified index from the collection |
टेक्निकल डिटेल
डोम संस्करण: | कोर स्तर 2 दस्तावेज़ वस्तु |
---|---|
प्रतिलाभ की मात्रा: | एक HTMLOptionsCollection ऑब्जेक्ट, <select> तत्व में सभी <विकल्प> तत्वों का प्रतिनिधित्व करता है। संग्रह में तत्वों को क्रमबद्ध किया जाता है क्योंकि वे स्रोत कोड में दिखाई देते हैं |
और ज्यादा उदाहरण
उदाहरण
[ सूचकांक ]
ड्रॉप-डाउन सूची में पहले विकल्प (इंडेक्स 0) का टेक्स्ट प्राप्त करें:
var x = document.getElementById("mySelect").options[0].text;
एक्स का परिणाम होगा:
Apple
उदाहरण
आइटम ( सूचकांक )
ड्रॉप-डाउन सूची में पहले विकल्प (इंडेक्स 0) का टेक्स्ट प्राप्त करें:
var x = document.getElementById("mySelect").options.item(0).text;
एक्स का परिणाम होगा:
Apple
उदाहरण
नामित आइटम ( आईडी )
ड्रॉप-डाउन सूची में id="orange" के साथ विकल्प का टेक्स्ट प्राप्त करें:
var x = document.getElementById("mySelect").options.namedItem("orange").text;
एक्स का परिणाम होगा:
Orange
उदाहरण
ड्रॉप-डाउन सूची में सूचकांक स्थिति "1" पर "कीवी" विकल्प जोड़ें:
var x = document.getElementById("mySelect");
var c = document.createElement("option");
c.text = "Kiwi";
x.options.add(c, 1);
उदाहरण
ड्रॉप-डाउन सूची से इंडेक्स "1" वाले विकल्प को हटा दें:
var x = document.getElementById("mySelect");
x.options.remove(1);
उदाहरण
ड्रॉप-डाउन सूची में सभी विकल्पों के माध्यम से लूप करें, और प्रत्येक विकल्प का टेक्स्ट आउटपुट करें:
var x = document.getElementById("mySelect");
var txt = "";
var i;
for (i = 0; i < x.length; i++) {
txt = txt + x.options[i].text + "<br>";
}
txt का परिणाम होगा:
Apple
Orange
Pineapple
Banana
उदाहरण
ड्रॉप-डाउन सूची में एक विकल्प चुनें और आईडी = "डेमो" वाले तत्व में चयनित विकल्प के टेक्स्ट को आउटपुट करें:
var x = document.getElementById("mySelect");
var i = x.selectedIndex;
document.getElementById("demo").innerHTML = x.options[i].text;
परिणाम हो सकता है:
Banana
उदाहरण
किसी अन्य ड्रॉप-डाउन सूची में चयनित विकल्प के आधार पर ड्रॉप-डाउन सूची में विकल्प बदलें:
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);
}
}
}
वस्तु का चयन करें