jQuery: dealing with a space in the id attribute

While it’s technically invalid to have a space in an ID attribute value in HTML (see @karim79’s answer), you can actually select it using jQuery.

See http://mothereffingcssescapes.com/#A%20B:

<script>
  // document.getElementById or similar
  document.getElementById('A B');
  // document.querySelector or similar
  $('#A\\ B');
</script>

jQuery uses a Selectors API-like syntax, so you could use $('#A\\ B'); to select the element with id="A B".

To target the element in CSS, you could escape it like this:

<style>
  #A\ B {
    background: hotpink;
  }
</style>

Leave a Comment