Properti indeks opsi
Contoh
Menampilkan indeks dan teks dari opsi yang dipilih dalam daftar drop-down:
var x = document.getElementById("mySelect").selectedIndex;
var y = document.getElementById("mySelect").options;
alert("Index: " + y[x].index + " is " + y[x].text);
Definisi dan Penggunaan
Properti indeks menetapkan atau mengembalikan posisi indeks opsi dalam daftar turun bawah.
Indeks dimulai dari 0.
Dukungan Peramban
Property | |||||
---|---|---|---|---|---|
index | Yes | Yes | Yes | Yes | Yes |
Sintaksis
Kembalikan properti indeks:
optionObject.index
Setel properti indeks:
optionObject.index = integer
Nilai properti
Value | Description |
---|---|
integer | Specifies the index position of the option within a drop-down list |
Detail Teknis
Nilai Kembali: | Angka, mewakili posisi indeks opsi dalam daftar drop-down. Indeks dimulai dari 0. |
---|
Lebih Banyak Contoh
Contoh
Menampilkan teks dan indeks semua opsi dalam daftar drop-down:
var x = document.getElementById("mySelect");
var txt = "All options: ";
var i;
for (i = 0; i < x.length; i++) {
txt = txt + "\n" + x.options[i].text + " has index: " + x.options[i].index;
}
Objek Opsi