Herland Cid
Herland Cid

Reputation: 574

Handle an Array of JQuery elements to interact with checkbox

What I am basicly trying to do, is to build an array of JQuery elements in order to interact with the checkboxes, the elements are $(.arms) and $(.neck)

Javascript:

var elements = new Array ($('.arms'),$('.neck'));

for (var i=0; i < elements.length; i++) {

elements[i].change = function() {

elements[i].not(this).prop('disabled',this.checked);

});

}

HTML:

<input type="checkbox" id="15000" class="arms"/>$50<br/>
<input type="checkbox" id="9400" class="arms"/>$60<br/>
<input type="checkbox" id="9500" class="neck"/>$70<br/>
<input type="checkbox" id="9600" class="neck"/>$80<br/>

Here is the Fiddle

Here is what is supposed to be doing, but only works with 1 element: one element fiddle

I think I am pretty close but something is not working.

Upvotes: 1

Views: 591

Answers (2)

Roko C. Buljan
Roko C. Buljan

Reputation: 206209

LIVE DEMO

$('.arms, .neck').change(function(){
    var myClass = $(this).attr('class'); 
    $('.'+myClass).not(this).prop('disabled', this.checked); 
});

Upvotes: 1

QuentinUK
QuentinUK

Reputation: 3077

$(".arms, .neck").on("change", function() {

    $(".arms, .neck").not(this).prop('disabled',this.checked);
});

Do the classes together, jQuery handles the arrays.

Upvotes: 0

Related Questions