How to escape ":"?

IAdapter picture IAdapter · Jan 25, 2011 · Viewed 18.4k times · Source

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"))

Answer

jAndy picture jAndy · Jan 25, 2011

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;
    });
}