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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s