jqueryjsfescapingseam

How to escape ":"?


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

Solution

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