0
jQuery("input[name=a.b.c]")

Executing this line using jQuery 1.10.2 or 1.9.1 results in the message:

"Syntax error, unrecognized expression: input:hidden[name=a.b.c]".

I understand the core problem which is that the dots are not escaped or quoted out. This would work:

jQuery("input[name='a.b.c']")

The constraint is that I do not have the ability to change the line of code with the bad selector. That line is produced by the website (which I don't own) and they don't give me the ability to change that.

However, they do allow me to add arbitrary JS files to the header of the page (which means I can use a different jQuery version or even edit the jQuery file). My question is whether anyone knows another way around this so that jQuery can cope without the quotes since I cannot change the bad code.

For those saying that I can just change the name, this doesn't help because the JS still throws an error because changing the name of the element doesn't fix the bad selector.

Thanks

Kias
  • 841
  • 9
  • 22
  • 1
    if you can't change name,can you change jquery selector or not????? – rajesh kakawat Dec 21 '13 at 11:17
  • Got any more info about how you're getting this code? This is the problem, and perhaps we need to create a better solution for it – Richard Peck Dec 21 '13 at 11:29
  • jquery is using CSS selector with Sizzle JS implementation. add quotes for this to work. – devBinnooh Dec 21 '13 at 17:42
  • The code is coming from the webserver generating the HTML. Embedded in the HTML is the bad javascript code. Unfortunately I cannot edit that bad code because they do not give much customization options. The one possibly saving customization they do allow is for me to add arbitrary JS files in the header. I'm wondering if someone smarter than me knows how I could possible adapt jQuery source to handle this bad selector without throwing an exception. – Kias Dec 21 '13 at 21:38

3 Answers3

2

The proper way of executing this selector is:

jQuery('input[name="a.b.c"]')

Obviously you need to edit the algorithm that creates this line, there's no way jquery will accept an invalid selector.

  • I know that this is how you should do it (after all, I did post this solution in my question). The problem is that I do not have control of that code so no matter what I do the problem code will get executed. My question is how to set things up so that an error isn't caused. – Kias Dec 21 '13 at 21:30
  • Turns out jQuery 1.3.2 copes with the line okay even without the quotes. But that version is from 2009 and won't work with a number of jQuery plugins. I wonder what allows it to cope with the bad selector while later versions cannot. – Kias Dec 21 '13 at 21:31
1

Take a look here.

How do I extend jQuery's selector engine to warn me when a selector is not found?

In your case I would do something like this.

var oldInit = $.fn.init;
$.fn.init = function(selector, context, rootjQuery) {
    selector = fixItWithQuotes(selector, context, rootjQuery);
    return new oldInit(selector, context, rootjQuery);
};

untested by me, but it should give you an idea. Also, this might give you more ideas?

http://blog.tallan.com/2012/01/17/customizing-the-default-jquery-selector-behavior/

Hope that makes sense.

Community
  • 1
  • 1
Sumit
  • 1,661
  • 1
  • 13
  • 18
0

Why don't you change the name attribute yourself?

var el = $("input"); 
el.attr("name", el.attr("name").replace(/[\d\.]+/g, "")); 
console.log(el.attr("name"));

Then change it back if you need to. jsFiddle here

symlink
  • 11,984
  • 7
  • 29
  • 50
  • The problem is not the name of the element, it's that the line of javascript throws an error. Changing the name of the element will not prevent the error. – Kias Dec 21 '13 at 21:33