admin管理员组文章数量:1180433
jQuery selector can't select an ID if it contains a .
in it.
in my application ID name generates dynamically from usernames.
How can I escape any special character from tag IDs so that jQuery selector works well?
for example, ID1: This.Is.ID.1
ID2: This.is.Another.ID
Can anyone help me.
Thanks in advance.
jQuery selector can't select an ID if it contains a .
in it.
in my application ID name generates dynamically from usernames.
How can I escape any special character from tag IDs so that jQuery selector works well?
for example, ID1: This.Is.ID.1
ID2: This.is.Another.ID
Can anyone help me.
Thanks in advance.
Share Improve this question edited Oct 25, 2013 at 18:08 Pars asked Oct 25, 2013 at 18:03 ParsPars 5,26211 gold badges59 silver badges91 bronze badges 5- 1 Regex escape is a single \ – Rory McCrossan Commented Oct 25, 2013 at 18:04
- 1 could you please write full line code of regex. – Pars Commented Oct 25, 2013 at 18:06
- 3 What exactly is the situation where you need this? First you talk about jQuery selectors, but then you ask how to escape dots in regex. – JJJ Commented Oct 25, 2013 at 18:06
- jQuery selector can't select, so I need to escape IDs to tell jquery work with escaped IDs. I think this is the way, not sure ... :( – Pars Commented Oct 25, 2013 at 18:07
- 1 RTFM: api.jquery.com/category/selectors "To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\." – Reeno Commented Oct 25, 2013 at 18:11
5 Answers
Reset to default 17If I understand you correctly, you want to modify a string that contains periods to have \\ in front of every period, to be supported as an id in the jQuery selector. Here is how to do that:
var username = 'some.username.with.dots';
// Replace all periods with \\. to
username = username.replace(/\./g, '\\\\.');
// find element that matches #some\\.username\\.with\\.dots
$('#' + username).doSomethingWithjQuery();
.
means "any character" in regex, so you need to escape it by putting\
in front.- The
g
regex modifier means greedy, without it the replace expression would only replace the first.
with\\.
Edit
I tried my code, and it seems like you need to change the replace value to \\\\.
to make it become \\.
The official jQuery FAQ has that exact question: How do I select an element by an ID that has characters used in CSS notation? They even have a function to help you escape. Quoting it:
... The colon ("
:
") and period (".
") are problematic within the context of a jQuery selector because they indicate a pseudo-class and class, respectively.In order to tell jQuery to treat these characters literally rather than as CSS notation, they must be "escaped" by placing two backslashes in front of them.
// Does not work:
$( "#some:id" )
// Works!
$( "#some\\:id" )
// Does not work:
$( "#some.id" )
// Works!
$( "#some\\.id" )
The following function takes care of escaping these characters and places a "#" at the beginning of the ID string:
function jq( myid ) {
return "#" + myid.replace( /(:|\.|\[|\]|,|=)/g, "\\$1" );
}
The function can be used like so:
$( jq( "some.id" ) )
Use \\
(double backslash) to escape special characters.
$('#this\\.something')
Fiddle
The the jQuery documentation states that two backslashes are used to escape them.
So your example would use $("#This\\.Is\\.ID\\.1")
You don't actually need to remote the .'s from the id. You simply need to escape any special characters in the selector. For example, to select the following element:
<div id='foo.bar'></div>
... you can use:
$('#foo\\.bar');
本文标签: jqueryescape dot in javascriptStack Overflow
版权声明:本文标题:jquery - escape dot in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738150634a2066130.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论