0

I'm using jQuery to add a class name to an element, but when the class name contains special characters like '&', or '/', it throws the error seen below:

function cleanString(string) {
  if (string) {
    var cleanString = string.replace(/\s+/ig, "");
    return cleanString;
  }
  else {
    return "Uncategorized";
  }
}

var team = nullCheck(_data[i].title)
var teamCleanString = cleanString(team);
$('#jobs-container .jobs-teams').append(
    '<a href="#" class="btn '+teamCleanString+'">'+team+'</a>'
  );
}

Produces this:

Uncaught Error: Syntax error, unrecognized expression: .Marketing&Communications
eclipsis
  • 1,541
  • 3
  • 20
  • 56

1 Answers1

2

There are certain characters that have functionality that are not valid for naming CSS selectors without escaping.

Which characters are valid in CSS class names/selectors?

You can use these special characters in naming if you perform proper escaping.

https://mathiasbynens.be/notes/css-escapes

Try using .Marketing\&Communications

Community
  • 1
  • 1
Mauin
  • 35
  • 7