Kaines Kaines - 1 year ago 61
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.

Answer Source

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")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download