Reputation: 3181
I already know how to select DOM elements with IDs which contain dots, and I've read all the related answers on stackoverflow (like this: How to select html nodes by ID with jquery when the id contains a dot?), but the problem is there's lots of plugins out there that are not aware of this issue, and short of going through the code of all the plug-ins we're using and replace the selectors appropriately, I was thinking that maybe there's another plug-in/extension for jQuery which replaces the main selector mechanism to make it dot-safe. I am using plug-ins like jqGrid and I've seen many places in the code where something like this is used:
$('#' + id).somefunction()
Any ideas?
Thanks in advance
Upvotes: 2
Views: 597
Reputation: 41581
You can alter the $.fn.init
function to change the behavior of jQuery, but there isn't a quick and easy way to make sure IDs with dots in them are safe throughout your application. A quick and dirty solution would replace all .
s with \\.
by altering the init
function, but then all your class selectors break.
The only solid way to do it is passing in the IDs with escaped dots to all your plugins.
EDIT
If you do want a quick and dirty solution, here's one that will at least work as long as you don't have something like #id.class
. It will work for #id .class
(the space in between).
(function($){
$.fn._init = $.fn.init;
$.fn.init = function ( selector, context, rootjQuery ) {
if (typeof selector == 'string' && selector.match('#')) {
selector = selector.replace(/\#([^\s\.]*)\./g, '#$1\\.')
}
return new $.fn._init(selector,context,rootjQuery);
}
})(jQuery);
Upvotes: 1