12

All of a sudden some UI functionlities in our site are not working and I'm getting the error message:

jQuery uncaught exception: syntax error, unrecognized expression [ tabindex="something"]

THIS IS MY CODE:

var thumb_src = jQuery('a[name="thumb-image"] img[src*=' + sku + ']').attr('src');
jQuery( 'a[ tabindex=' + thumb_src + ']' ).prevAll().removeClass('selectedThumb');
jQuery( 'a[ tabindex=' + thumb_src + ']' ).addClass( 'selectedThumb' );
jQuery( 'a[ tabindex=' + thumb_src + ']' ).nextAll().removeClass('selectedThumb');

It was working fine until jQuery was upgraded to the latest and I believe that is the cause. Am I doing something illegal in the statements above? Thanks for any input or help on this!

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
IntricatePixels
  • 1,219
  • 6
  • 29
  • 55
  • i think you have to use `a[tabindex=` rather than `a[ tabindex=` – drudge May 05 '11 at 21:12
  • What versions did you upgrade from/to? I'm guessing you're using something that is deprecated in the new version. – WEFX May 05 '11 at 21:13
  • why would you be setting your tabindex to something that isn't a number? also have you tried removing the whitespace here "a[ t". – jacobangel May 05 '11 at 21:14
  • Also, you can test the different versions of jQuery framework here - **[link](http://jsfiddle.net/eB9TL/)** – WEFX May 05 '11 at 21:16
  • 1
    I removed the space between a[tabindex. Also changed tabindex to title but still same error message. Changed the .attr to .prop on the first line of code as well but still same error message. – IntricatePixels May 05 '11 at 21:28
  • @WEFX upgraded to version 1.6 – IntricatePixels May 05 '11 at 21:45

2 Answers2

15

Most likely any . or / characters in your thumb_src are breaking the attribute selectors in your last three lines as they are special CSS characters.

Try the double quotes inside those selectors so they're taken literally (even though you really shouldn't be using anything but numeric values for tabindex):

jQuery('a[tabindex="' + thumb_src + '"]')

The API docs say that these quotes are mandatory in jQuery attribute selectors anyway.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • That's exactly what it was. 5 years + coding and programming and still missing obvious things like this! However this also prompted me to update the .attr() to .prop() too! Thanks a lot! – IntricatePixels May 05 '11 at 22:37
  • No problem - I was rather puzzled with everyone going on about `.attr()` and `.prop()` although that's still a necessary change when upgrading to 1.6. I just didn't think it was related to your error. – BoltClock May 05 '11 at 22:39
  • brilliant! My issue happened to be that i was trying to subtract 1 from "thumb_scr" and it was freeking out so what i did was ```$('[tabindex="'+(thumb_src - 1)+']")``` note the brackets separate the math from the rest of the string – James Harrington Aug 20 '14 at 20:49
2

The attr() function was changed as of jQuery 1.6, use prop() instead:

var thumb_src = jQuery('a[name="thumb-image"] img[src*=' + sku + ']').prop('src');

See this question

Community
  • 1
  • 1
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • This changed in jQuery 1.6, read the release notes here: http://blog.jquery.com/2011/05/03/jquery-16-released/ – CamelCamelCamel May 05 '11 at 21:18
  • Changed it to .prop() but still same error message. I feel like it's not liking the a[tabindex=" part but no idea why. Current jQuery version is 1.6 – IntricatePixels May 05 '11 at 21:27
  • hmmm im not sure. can you post part of you code (or all of it if possible) to jsfiddle? – Naftali May 05 '11 at 21:28
  • Neal [link](http://www.golfballnut.com/products/bridgestone-tour-b330-golf-balls) is an example. The error is triggered when you hover over the image thumbnails and the js is on line 920. – IntricatePixels May 05 '11 at 21:31
  • Changing all .attr() to .prop() throughout the JS made the UI functionalities partially work again but still throwing the error message. – IntricatePixels May 05 '11 at 21:44
  • 2
    What does this have to do with `.attr()` versus `.prop()`? – BoltClock May 05 '11 at 22:01