Friday, December 28, 2012

onchange

Event handler onchange (Bubbles: No, Cancelable: No) terjadi ketika adanya selection, checked state atau isi-isi suatu elemen sudah berubah.

Berikut contoh meregister event handler onchange via 'inline model':

Warna kesukaan anda: <SELECT ONCHANGE="this.options[this.selectedIndex];document.body.style.backgroundColor=this.value;"> <OPTION VALUE="white">Putih <OPTION VALUE="red">Merah </SELECT>

Berikut contoh meregister event handler onchange via 'traditional model':

Warna kesukaan anda: <SELECT ID="selectID"> <OPTION VALUE="white">Putih <OPTION VALUE="red">Merah </SELECT> <SCRIPT TYPE="text/javascript"> function HandleEvent() { this.options[this.selectedIndex]; document.body.style.backgroundColor=this.value; } var selectElem=document.getElementById("selectID"); selectElem.onchange=HandleEvent;
</SCRIPT>

Berikut contoh meregister event handler onchange via DOM:

Warna kesukaan anda: <SELECT ID="selectID"> <OPTION VALUE="white">Putih <OPTION VALUE="red">Merah </SELECT> <SCRIPT TYPE="text/javascript"> function HandleEvent() { this.options[this.selectedIndex]; document.body.style.backgroundColor=this.value; } var selectElem=document.getElementById("selectID"); selectElem.addEventListener("change", HandleEvent, false); </SCRIPT>