4

I have a select with id: param0.typeParameter.

I try to get it with jquery with:

$('#param0.typeParameter');

i get nothing but if it use this

document.getElementById('param0.typeParameter');

that work

4
  • typeParameter is supposed to be a css class ? Commented Nov 19, 2013 at 17:06
  • whats the difference? Maybe the ».« is no good idea… Commented Nov 19, 2013 at 17:06
  • 1
    You should just avoid using dot in your id name. Dot are used to define a class Commented Nov 19, 2013 at 17:10
  • It's the spring way to manage a class with many field Commented Nov 19, 2013 at 17:11

3 Answers 3

14

To make jQuery select element with ID param0.typeParameter instead of element with ID param0 and class name typeParameter you should use the following:

$('#param0\\.typeParameter');

Official Proof

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^``{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\. For example, an element with id="foo.bar", can use the selector $("#foo\\.bar").

SOURCE: http://api.jquery.com/category/selectors/

DEMO: http://jsfiddle.net/LtRxg/

Sign up to request clarification or add additional context in comments.

Comments

10

You can try this instead:

$('[id="param0.typeParameter"]');

Or else the code assumes you want to select an element with id param0 and class typeParameter

4 Comments

I believe this makes it a generic attribute selector that will be magnitudes slower than a selector with a properly escaped period that can make use of retrieving the element by its ID.
Hmmm that may be true @Deadron but how slow is "slow"? Is it really that noticeable?
also, it's selecting an element by id. Since there should only be one of them, you likely only need to perform this once or twice, meaning even if the difference in speed was large, it likely won't be noticeable.
@qwertynl While it is obviously implementation specific an ID selection can be assumed to be a constant time operation while a attribute selector would have to scan the document. Depending on the size of your document and the number of times the selector is executed it could have an impact that is trivially avoided.
5

jQuery uses the CSS-selector language. Which causes "#param0.typeParameter" to be interpretted as an element with id param0 and class typeParameter.

The jQuery statement is more similar to:

document.querySelector("#param0.typeParameter");

1 Comment

This is not the solution but the best answer cause of the explanation !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.