1

This seems like it should be simple, so not sure what i'm missing

// this works
$("#Q270_1, #Q271_1").change(function () {
    alert("Check 1")
});

// this works
var m1c1 = "#Q270";
$(m1c1+"_1").change(function () {
    alert("Check 2")
});

// This doesn't work
var m1c2 = "#Q271";
$(m1c1+"_1",m1c2+"_1").change(function () {
    alert("Check 3")
});

Here is a jsfiddle where it shows the above: https://jsfiddle.net/9Led5cv9/

Any assistance much appreciated. Thanks!

1
  • You need to include the , within the quotes, as it's part of the selector string. Additionally, you need to concatenate the m1c2 with a +. $(m1c1+"_1,"+m1c2+"_1") Commented Oct 3, 2016 at 23:59

2 Answers 2

2

You are creating 2 parameters which jQuery interprets as:

$(selector, context)

This would be the same as $(context).find(selector)

You need to concatenate the full string instead keeping the comma inside quotes

$(m1c1+ "_1, " + m1c2+"_1")
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, there I go not understanding the basics! Updated fiddle with if anyone else comes across this issue: jsfiddle.net/9Led5cv9/3
even simpler is give those elements a common class. Will make it easier reading the code later on
0

stringify that comma:

$(m1c1+"_1, "+m1c2+"_1").change(function () {
    alert("Check 3")
});

Now the 1 argument references both elements.

Note: I'd give the elements a class, instead of generating long complicated lists of jQuery selectors.

Comments

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.