16

for example I have id like

someform:somepanel:somebutton

When I do jQuery("#someform:somepanel:somebutton") it returns someform, how to AUTOMATICALLY escape that id?

EDIT:

I want to do something like this

jQuery(somefunction("#someform:somepanel:somebutton"))
IAdapter
  • 62,595
  • 73
  • 179
  • 242

7 Answers7

18

If it's only this very specialized version, you can just .replace() the character.

function somefunction(selector) {
    return selector.replace(/:/, '\\\\:');
}

jQuery(somefunction("#someform:somepanel:somebutton"))

is then converted into

jQuery("#someform\\:somepanel\\:somebutton");

To have a more generic version, you can use a regexp:

function somefunction(selector) {
    return selector.replace(/(!|"|#|\$|%|\'|\(|\)|\*|\+|\,|\.|\/|\:|\;|\?|@)/g, function($1, $2) {
        return "\\\\" + $2;
    });
}
jAndy
  • 231,737
  • 57
  • 305
  • 359
  • I found the function parameters confusing. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter gives a good explanation. – elewinso Sep 07 '16 at 13:53
9

use the double backslashes:

 jQuery("#someform\\:somepanel\\:somebutton")

Related:


Update #1

After your comment in regards to auto escaping the best method I see is to create a function within the string object like so

String.prototype.escape = function()
{
    return this.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/])/g,'\\$1')
}

you can also specifically define a function for the colons like so:

String.prototype.escape_colon = function()
{
     return this.replace(/:/,'\\$1')
}

and use like so:

jQuery("someform:somepanel:somebutton".escape())

but this will cause issues on pseudo selectors such as:

jQuery("someform:somepanel:somebutton:first".escape())

the :first selector will be escaped and therefore you will not find your element.

but y our best bet will be to build a string parser within the prototype to replace where it finds a specific set of chars such as:

jQuery("someform(_e(:))somepanel(_e(:))somebutton:first".escape())

this way you can define what you want to escape, but if that was the case you may as well escape them yourself.

Community
  • 1
  • 1
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
5

Try:

jQuery("#someform\\:somepanel\\:somebutton")
VoodooChild
  • 9,776
  • 8
  • 66
  • 99
2

If you use PrimeFaces, they have a handy helper function to do just that:

escapeClientId(id) - Escaped JSF ids with semi colon to work with jQuery.

To call it:

PrimeFaces.escapeClientId("someform:somepanel:somebutton")

which returns:

#someform\\:somepanel\\:somebutton

Internally, it just calls replace(/:/g,"\\:") and adds the #, so you could use that, too.

sleske
  • 81,358
  • 34
  • 189
  • 227
1

I have created a function to escape colons for JSF in jQuery:

//USAGE: $(espaceIdForJSF('#someId:anotherId'));
function escapeIdForJSF(id) {
   return id.replace(/:/g,"\\:").replace(/\./g,"\\.");
}
Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
1

Use this trick: jQuery($('myid'))

Reason: I'm using "prototype" to look up element by id, then I pass result to jQuery.

Pros: easier to read. Cons: need Prototype and jQuery, but RichFaces uses Prototype anyway.

sth
  • 222,467
  • 53
  • 283
  • 367
gorlok
  • 1,155
  • 6
  • 16
0

JQuery 3.0 provides an escape function:

https://api.jquery.com/jQuery.escapeSelector/

$( "#" + $.escapeSelector( evilId ) );
oleh
  • 820
  • 2
  • 13
  • 28