Home >Web Front-end >JS Tutorial >How to Select Elements with Periods in Their IDs Using jQuery
In the context of ASP.NET MVC, where elements in forms often have periods in their IDs, selecting them using jQuery can pose challenges.
Consider the following situation:
<select id="Address.State">
To select this element using jQuery, one might attempt the following:
$("#Address.State").fillSelect(data);
However, this does not work because the period is a special character in jQuery selectors. To use it, you must escape it with a backslash:
$("#Address\.State").fillSelect(data);
This is because backslash is the escape character in JavaScript strings, and jQuery selects elements based on CSS selectors, which also use backslash as an escape character. Therefore, you need two backslashes to escape the period properly.
Alternatively, you can also consult the jQuery FAQ for additional guidance on selecting elements with special characters in their IDs: https://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/
The above is the detailed content of How to Select Elements with Periods in Their IDs Using jQuery. For more information, please follow other related articles on the PHP Chinese website!