Varun Jain
Varun Jain

Reputation: 1911

Iterating over checkboxes to retrieve value of checked ones

I have the following fiddle in jquery :

 var checks = $('input[name= "ScheduleCharge[hspecialty][]"]:checked');
 $(checks).each(function(){ 
     alert($(this).value()); 
 }); 

This just refreshes the page . If I replace $(this).value() with 1 , 1 is alerted correct number of times as per the number of checkboxes. Any suggestions ?

Upvotes: 0

Views: 39

Answers (2)

Sushanth --
Sushanth --

Reputation: 55750

Here $(this) is a jQuery Object and value is not valid property for it

Use .val() instead

alert($(this).val()) ;  $(this)  jQuery Object 

If you want to use the value property use the DOM object

alert( this.value ) ;   this DOM Object

Upvotes: 0

Adil
Adil

Reputation: 148120

You need to use val() instead of value() as value() is not defined by jquery and error would be causing refresh of page.

var checks = $('input[name= "ScheduleCharge[hspecialty][]"]:checked');
$(checks).each(function(){ 
    alert($(this).val()) ; 
}) ; 

Upvotes: 3

Related Questions