Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"))
like image 993
IAdapter Avatar asked Jan 25 '11 09:01

IAdapter


People also ask

Can we escape the reality?

While we may not be able to jump on the next plane to an island-getaway, we all can escape from reality mentally. Because every mind is different, it may take some trial and error to find out how you best are able to mentally escape reality.

What does it mean to mentally escape?

Escapism is mental diversion from unpleasant or boring aspects of daily life, typically through activities involving imagination or entertainment. Escapism may be used to occupy one's self away from persistent feelings of depression or general sadness.


2 Answers

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;
    });
}
like image 69
jAndy Avatar answered Oct 05 '22 07:10

jAndy


use the double backslashes:

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

Related:

  • jQuery selector value escaping
  • When do I need to escape metacharectars? (jQuery Selectors)
  • http://api.jquery.com/category/selectors/

    • If you wish to use any of the meta-characters ( such as !"#$%&'()*+,./:;?@[\]^{|}~ ) as a literal part of a name, you must escape the character with two backslashes: \\. For example, if you have an an element with id="foo.bar", you can use the selector $("#foo\\.bar"). The W3C CSS specification contains the complete set of rules regarding valid CSS selectors.

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.

like image 29
RobertPitt Avatar answered Oct 05 '22 07:10

RobertPitt