0

I expected that selector will be $("a.3") but it was not.

var giftId = '3';
$("a."+giftId).click(function() {
    ...
});

Can not I make like this?

Even if class name is not starts with digit it still does not concatenated

<a class="gift6"...

    var giftId = '6'; 
    $("a.gift"+giftId).click(...
thinker
  • 402
  • 1
  • 6
  • 15

1 Answers1

2

You can't have classes which start with a number, which would explain why this isn't working.

https://www.w3.org/TR/CSS2/syndata.html#value-def-identifier

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

Curtis
  • 101,612
  • 66
  • 270
  • 352
  • ok, what about this `var giftId = '6'; $("a.gift:"+giftId).click(... ` ? this still does not concatenated and class name not started with digit – thinker Jan 04 '17 at 11:27
  • @thinker `6` isn't a valid pseudo class so you can't do that – Curtis Jan 04 '17 at 11:35
  • @thinker I would suggest having a data attribute on your element, `data-giftid` and then you can do: `$("a.gift[data-giftid=" + giftId + "]")` – Curtis Jan 04 '17 at 11:36