Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: dealing with a space in the id attribute

Tags:

jquery

I have an element with id="A B". The following code fails:

<input type="text" id="A B">
<script>$("#A B").click(function(){alert();});</script>

The following code does not fail:

<input type="text" id="AB">
<script>$("#AB").click(function(){alert();});</script>
like image 519
Randomblue Avatar asked Jul 23 '11 19:07

Randomblue


People also ask

Can id attribute 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 an id have a space JavaScript?

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

Can we add id using jQuery?

We can add an id to an HTML element using jQuery very easily by combining the attr() method with a click event. Let's say we have the following HTML code and we want to give the user the ability to add an id to the paragraph. The new id will underline the paragraph content.

What does .id do in JavaScript?

The id attribute specifies a unique id for an HTML element. The value of the id attribute must be unique within the HTML document. The id attribute is used to point to a specific style declaration in a style sheet. It is also used by JavaScript to access and manipulate the element with the specific id.


3 Answers

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>
like image 147
Mathias Bynens Avatar answered Sep 21 '22 21:09

Mathias Bynens


Neither HTML4 nor HTML5 allows space characters in ID attribute values.

The HTML 4.01 spec states that ID 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 (.). For the class attribute, there is no such limitation. Classnames can contain any character, and they don’t have to start with a letter to be valid.

HTML5 gets rid of the additional restrictions on the id attribute. The only requirements left — apart from being unique in the document — are that the value must contain at least one character (can’t be empty), and that it can’t contain any space characters.

Source:

http://mathiasbynens.be/notes/html5-id-class

like image 39
karim79 Avatar answered Sep 22 '22 21:09

karim79


As they mentioned you shouldn't use spaces in id. But anyway, wouldn't this be working for you?

$("[id='A B']").click(...)

EDIT: yes it works, tested it!!! Don't kill me, standard-lovers!! ;-P

like image 42
Tomas Avatar answered Sep 22 '22 21:09

Tomas