0

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.

3
  • Is this you want? $("#" + MLSAreaMajorVariable + LoopVariable).prop("checked", anothervariable) Commented May 26, 2015 at 18:47
  • $("#"+variable+loopvariable).prop("checked", anothervariable); Commented May 26, 2015 at 18:47
  • Is that all the MLSAreaMajor elements, or a subset of them? Commented May 26, 2015 at 18:49

1 Answer 1

1

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); }
Sign up to request clarification or add additional context in comments.

1 Comment

This worked, thanks. Just one comment for other novices like me, I was passing the condition as 'false' instead of false with no quotes, and that caused a problem too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.