0

In the function below, I'd like the idurl be the word apple with a suffix. The suffix is number containing a number. Example would be apple12.

But in the function below, I have trouble concatenating the variable number with the word apple.

function apple(number) {
  if (something === "0") {
    $('.onchange :checkbox[idurl="apple" + number]').prop('checked', false);
  } else {
    $('.onchange :checkbox[idurl="apple" + number]').prop('checked', true);
  }
};

I tried using extra " or [] but it hasn't been very succesfull so far. The variable number is not bein recognized as a variable and thus not concatenated with the word apple.

Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
Carnifex
  • 57
  • 1
  • 7
  • 2
    how would you concatenate a string to a variable? It's the same thing. The selector is just a string. – scrappedcola Nov 07 '14 at 16:18
  • 1
    It's not a duplicate because that question asks how to go about mixing variables and selectors. This question asks why the variable isn't being added to the output string. This question asks for assistance with code, the other asks about the practice –  Nov 07 '14 at 16:28

3 Answers3

1

use

$('.onchange :checkbox[idurl="apple"' + number+']').prop('checked', false);

Breaking of string:

1st part '.onchange :checkbox[idurl="apple"'

2nd part number

3rd part of string ']'.

Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
1

I believe it's because it is inside your single quotes. Try this instead:

$('.onchange :checkbox[idurl="apple"' + number + ']').prop('checked', false);
EricBellDesigns
  • 955
  • 1
  • 9
  • 33
1

You need to call the function with an actual number where your number argument in the function is. Also, javascript doesn't recognize variables in quotes, so you need to escape your single quotes.

$('.onchange :checkbox[idurl="apple"' +number +']').prop('checked', false);