Pilihan Datalist Koleksi
Contoh
Cari tahu berapa banyak opsi yang ada di elemen <datalist> tertentu:
var x = document.getElementById("browsers").options.length;
Hasil dari x menjadi:
5
Lebih banyak contoh "Coba Sendiri" di bawah ini.
Definisi dan Penggunaan
Koleksi opsi mengembalikan koleksi semua opsi dalam elemen <datalist>.
Catatan: Elemen dalam koleksi diurutkan seperti yang muncul di kode sumber.
Dukungan Peramban
Collection | |||||
---|---|---|---|---|---|
options | Yes | 10.0 | Yes | Yes | Yes |
Sintaksis
datalistObject.options
Properti
Property | Description |
---|---|
length | Returns the number of <option> elements in the collection. Note: This property is read-only |
Metode
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 |
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 |
Detail Teknis
Versi DOM: | Objek Dokumen Tingkat 2 Inti |
---|---|
Nilai Kembali: | Objek HTMLCollection, mewakili semua elemen <option> dalam elemen <datalist>. Elemen-elemen dalam koleksi diurutkan seperti yang muncul dalam kode sumber |
Lebih Banyak Contoh
Contoh
[ indeks ]
Dapatkan nilai opsi pertama (indeks 0) dalam daftar data:
var x = document.getElementById("browsers").options[0].value;
Hasil dari x menjadi:
Internet Explorer
Contoh
barang ( indeks )
Dapatkan nilai opsi pertama (indeks 0) dalam daftar data:
var x = document.getElementById("browsers").options.item(0).value;
Hasil dari x menjadi:
Internet Explorer
Contoh
bernamaItem( nama_atau_id )
Dapatkan nilai opsi dengan id="google" di daftar data:
var x = document.getElementById("browsers").options.namedItem("google").value;
Hasil dari x menjadi:
Chrome
Contoh
Ulangi semua opsi dalam daftar data, dan keluarkan nilai opsi:
var x = document.getElementById("mySelect");
var txt = "";
var i;
for (i = 0; i < x.options.length; i++) {
txt = txt + x.options[i].value + "<br>";
}
Hasil dari txt akan menjadi:
Internet Explorer
Firefox
Chrome
Opera
Safari
Objek Daftar Data