22 January 2014

jQuery prop() Method

Kali ini saya akan membahas tentang salah satu method JQuery yaitu prop().
Gunanya untuk set atau get nilai dari property elemen yang dipilih.

Syntax

set property
$(selector).prop(property)

 
set property dan value
$(selector).prop(property,value)
set property dan value menggunakan sebuah function:
$(selector).prop(property,function(index,currentvalue))
Set multiple properties dan values
$(selector).prop({property:value, property:value,...})
Parameter Description
property nama property
value value dari property
function(index,currentvalue) function untuk mengembalikan value property yang di-set
  • index - indeks posisi dari elemen
  • currentvalue - current property value yang dipilih
 Ini contohnya:

misalkan checkbox ini sebelumnya belum dicentang, saya ingin
jika dicentang muncul alert "woiiii ini udah dicentang".
Maka gunakanlah prop().

HTML:
<input type="checkbox" class="cek"  > Add Storage 1 GB
 Javascript:
$(".cek").click(function(){
         if ($(this).prop('checked')==true)
        {
              alert("woiiii ini udah dicentang");
        }
});

Demikianlah tutorial di kesempatan berbahagia ini, kalau ada yang kurang dimengerti atau ada pertanyaan tulislah di kolom komentar di bawah.

Referensi: jquery.com , w3schools.com

No comments:

Contributors