JQuery selectors and spaces in ids

I know that the general response to the question about using JQuery selectors for node ids that have spaces in them is to not have spaces in ids.  According to the HTML specification, spaces are not allowed.

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens (“-“), underscores (“_”), colons (“:”), and periods (“.”).

As we live in a world where web browsers are lenient when it comes to standards, developers are inherently lazy, and you don't really want to rewrite too much of someone else's code just to add a simple feature.  You sometimes need to select nodes with ids that contain spaces.

The attribute selector is what you need.  e.g.

$('[id=An id with spaces]').click(function () {
    // Do something here
});

As a side note and something for me to remember.  The change() listener does not work all that well in Internet Explorer.  It's easier to use click()

Advertisements