Pilih add() Metode
Contoh
Tambahkan opsi "Kiwi" di akhir daftar tarik-turun:
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option);
Definisi dan Penggunaan
Metode add() digunakan untuk menambahkan opsi ke daftar drop-down.
Tip: Untuk menghapus opsi dari daftar drop-down, gunakan metode remove() .
Dukungan Peramban
Method | |||||
---|---|---|---|---|---|
add() | Yes | Yes | Yes | Yes | Yes |
Sintaksis
selectObject.add(option, index)
Nilai Parameter
Parameter | Description |
---|---|
option | Required. Specifies the option to add. Must be an option or optgroup element |
index | Optional. An integer that specifies the index position for where the new option element should be inserted. Index starts at 0. If no index is specified, the new option will be inserted at the end of the list |
Detail Teknis
Nilai Kembali: | Tidak ada nilai kembali |
---|
Lebih Banyak Contoh
Contoh
Tambahkan opsi "Kiwi" di awal daftar tarik-turun:
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option, x[0]);
Contoh
Tambahkan opsi "Kiwi" di posisi indeks "2" dari daftar drop-down:
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option, x[2]);
Contoh
Tambahkan opsi sebelum opsi yang dipilih dalam daftar drop-down:
var x = document.getElementById("mySelect");
if (x.selectedIndex >= 0) {
var option = document.createElement("option");
option.text = "Kiwi";
var sel = x.options[x.selectedIndex];
x.add(option, sel);
}
Pilih Objek