Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a DOM element have an ID that contains a space?

Tags:

And if so, how do you select on with jQuery? I.e., $("#a b") looks for all <b> nodes within the node with id=a.

like image 311
Tom Lehman Avatar asked Sep 08 '09 03:09

Tom Lehman


People also ask

Can element ID have space?

id 's value must not contain whitespace (spaces, tabs etc.). Browsers treat non-conforming IDs that contain whitespace as if the whitespace is part of the ID. In contrast to the class attribute, which allows space-separated values, elements can only have one single ID value.

Can JavaScript ID have spaces?

3.1 The id attribute: The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.

Do DOM elements have unique IDs?

OK, there is no ID associated to DOM element automatically. DOM has a hierarchycal structure of elements which is the main information. From this perspective, you can associate data to DOM elements with jQuery or jQLite. It can solve some issues when you have to bind custom data to elements.


2 Answers

According to the HTML 4.0 specification for basic types:

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 (".").

And even if spaces were valid, an id attribute with spaces would be interpreted by jQuery as an ancestor descendant selector with the current selector syntax.

like image 66
karim79 Avatar answered Sep 22 '22 07:09

karim79


Short answer is no. Letters, numbers, underscores, hyphens, periods and colons only.

(darn, karim beat me to it.)

like image 38
Gabriel Hurley Avatar answered Sep 21 '22 07:09

Gabriel Hurley