Kaines Kaines - 18 days ago 5x
jQuery Question

How to concatenate multiple variable and strings in functions in jquery

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:

Any assistance much appreciated.


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")