OahuInvestor
OahuInvestor

Reputation: 121

Insert variables in jQuery checkbox code

I have the following jquery code:

$("#MLSAreaMajor1").prop("checked", false);
$("#MLSAreaMajor2").prop("checked", false);
$("#MLSAreaMajor3").prop("checked", false);

I want to loop through this code and replace the MLSAreaMajor with a variable, the 1,2,3 after the MLSAreaMajor would be a variable based on loop number, and the false will also be a variable as it my say false or true.

I can't seem to get the correct syntax and have tried numerous things.

Upvotes: 0

Views: 34

Answers (1)

Eric Martinez
Eric Martinez

Reputation: 31787

It should be something like this

var someVariable = "MLSAreaMajor";
for(var i = 1; i <= someNumber; i++) {
    var condition = false; // check in here if it should be true or false
    $("#"+someVariable+""+i).prop("checked", condition); }

Upvotes: 1

Related Questions