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.

2
  • 2
    how would you concatenate a string to a variable? It's the same thing. The selector is just a string. Commented Nov 7, 2014 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 Commented Nov 7, 2014 at 16:28

3 Answers 3

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 ']'.

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.