0

I am trying to disable multiple fields in a checkbox survey at the same time. Is it possible to select by multiple classes on same the div? I tried below but its not working. The code works with one class.

function surveyInit(){
  $("div[class*='addressLine1', class*='addressLine2'] input"  ).attr('disabled', 'disabled');    
}
2
  • 4
    Do the classes really contain addressLine1 or is it exactly addressLine1? Commented Feb 18, 2020 at 9:07
  • 2
    Side note: .attr("disabled", "disabled") works, but .prop("disabled" true) is simpler, and lets you use .prop("disabled", false) to undo it (instead of .removeAttr("disabled")). Commented Feb 18, 2020 at 9:09

2 Answers 2

6

You need to use multiple selectors separated by commas, not put the commas inside the attribute selector.

function surveyInit(){
    $("div[class*='addressLine1'] input, div[class*='addressLine2'] input").attr('disabled', 'disabled');
}
Sign up to request clarification or add additional context in comments.

3 Comments

*= denotes Attribute Contains Selector hence $("div[class*='addressLine']") should be enough.
@Rayon That will also match addressLine3
Yeah, my bad. That depends on the use case.
-2

you need comma as seperator and you can youse dot to select class instead of attribute selector

function surveyInit(){
 $("div.addressLine1 input,div.addressLine2 input"  ).attr('disabled', 'disabled');

}

2 Comments

Note this is subtly different to the OP. [class*='addressLine1'] selects any element where the class contains addressLine1, your answer selects elements where the class is exactly addressLine1 - hence my comment for clarification on the question.
Your comment to OP was not answered so i "assumed" addressLine12 is not desirable i gave other option for exact match to avoid possible bugs.

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.