JQuery selector problem - 'Syntax error, unrecognized expression' fix

I just spent a little time wrestling with an annoying JQuery error when trying to select a form element by name.

I was using the following code to simply grab the contents of a hidden field named 'required':

var req = $("input[@name=required]").val();

I got this syntax from a quick google search and found it referenced on a number of web guides about JQuery.

However, when I used it I got the following error in Firefox:

'Syntax error, unrecognized expression: [@name=required]' when calling method: ..... '

Well after some head scratching and then some more searching I found the answer. The @ syntax is no longer supported in JQuery 1.3+.

So the simple fix is to just remove the @ and it'll work as expected:

var req = $("input[name=required]").val();

Hopefully this will help someone who, like me, hit's Google when looking for a quick fix to a problem.. :)

Comments
Adam's Gravatar Thanks for this post... right at the top of my google search... saved me alot of time.
# Posted By Adam | 3/18/09 9:53 AM
James Allen's Gravatar Hey there Adam,
Thanks for your comment and glad you were able to find the answer to this issue quickly. I like being able to hit Google when I get an error and get the answer straight away. No hassle and then we can get right back to the creative part of our job. :)
# Posted By James Allen | 3/18/09 10:41 AM
hudz's Gravatar Hi..

Thanks too. But, we have to change 493 errors regarding '@'. Are you sure, there's nothing will happen?
# Posted By hudz | 3/21/09 10:33 AM
James Allen's Gravatar Yeah it should be fine, just be careful with your search and replace pattern - if you're doing a bulk change - so that you don't replace anything you shouldn't.

The @name syntax is certainly not supported anymore so replacing '@name' for 'name' should do the trick.

My advise is to first take a full copy of the site and then do the global search and replace. Test the site and if there are any problems roll back. Do it locally first if you can.
# Posted By James Allen | 3/21/09 12:54 PM
Brian FitzGerald's Gravatar Thanks a mil James! I recently updated to jQuery 1.3.2 and was baffled why my stuff was breaking all of a sudden. Your post helped me get it straightened out quickly
# Posted By Brian FitzGerald | 4/10/09 11:05 PM
James Allen's Gravatar Hey there Brian. Absolutely no problem - glad I could help. As soon as I found the solution I thought I'd get it posted as I knew others would come across it. One of those really annoying problems that isn't immediately obviously unless scouring the release notes which we don't always have the time to read (I tend to just keep on updating and usually things don't break). Well, as long as we all post up these things when we discover them the solutions will be available to everyone without too much hassle.. Gotta love the Internet. :)
# Posted By James Allen | 4/11/09 2:21 AM
Elmer's Gravatar Thank you very much, saved me a lot of time :-)
# Posted By Elmer | 4/26/09 7:35 AM
Doug B's Gravatar This post saved me a lot of headache. I had found an older code sample and was baffled as to why it wasn't working in my app when other selector statements were fine.

Thanks!

~Doug
# Posted By Doug B | 4/30/09 4:35 PM
James Allen's Gravatar Hey no problem Doug. I was in the exact same position as some code I found had the @ reference in it so I couldn't figure out what was wrong. Glad this saved you some time.
# Posted By James Allen | 4/30/09 6:03 PM
Adam's Gravatar Yup, googled and this page was #1 for this problem. Thanks for posting it here!
# Posted By Adam | 5/7/09 12:49 PM
James Allen's Gravatar Hey there Adam,
Excellent. Glad to know it was able to help you out quickly. Nothing better than finding the answer to a creativity destroying problem at the number slot on good old Google.
# Posted By James Allen | 5/7/09 5:11 PM
Erick's Gravatar Thanks man, nice to share knowledge and save us some time
# Posted By Erick | 5/15/09 11:03 PM
James Allen's Gravatar Hey no problem,
Thanks for taking the time to comment.
Nice to know this is helping people. The quicker this kind of problem is solved the better in this game.
# Posted By James Allen | 5/18/09 4:52 PM
Kirk's Gravatar Thanks for the entry... it was the second result of my Google search and saved me the rest of my afternoon (though I already spent about 2 hours trying to track this down). I guess that's what I get for keeping inconsistent versions of jQuery between the various projects I'm working on. The '@' syntax was working perfectly for me on another site, so I was losing my mind!
# Posted By Kirk | 6/4/09 10:16 PM
James Allen's Gravatar I know just what you mean! I hate those kind of inconsistencies - they can truly melt your brain. Annoying that they suddenly removed syntax which had worked for months. Just glad I was able to provide the answer for you.. Took me a while to track it down..
# Posted By James Allen | 6/4/09 11:30 PM
Patrice Fiset's Gravatar Thanks you just save me a lot of time.
# Posted By Patrice Fiset | 6/23/09 1:50 AM
James Allen's Gravatar My pleasure Patrice.
# Posted By James Allen | 6/30/09 12:52 PM
Martín's Gravatar Just like everyone else, I landed from Google searching for a solution to this problem. I was upgrading jQuery from 1.2.6 to 1.3.2. Removed the @s and everything worked again. Thank you, James!
# Posted By Martín | 6/30/09 4:36 PM
James Allen's Gravatar Awesome Martin! Quick Google solutions are the best...
# Posted By James Allen | 6/30/09 11:48 PM
Brian's Gravatar Ah, this was killing me for an entire afternoon. I wish I searched earlier. This was at the top of Google's results. Thanks!
# Posted By Brian | 7/21/09 9:04 PM
James Allen's Gravatar @Brian,
Hey no probs - glad I could help. Thanks for posting.
# Posted By James Allen | 7/21/09 9:25 PM
Daniel's Gravatar Same here. This was killing me - I had some sample jQuery that should _just work_, but got this error. It looked fine according to the official jQuery 1.2 docs still on the jQuery site, but I finally landed on your page for the fix. I did finally find reference to this here:

http://docs.jquery.com/Selectors

Still, seems like they should have a breaking change like that in blinking red all over their site =)
# Posted By Daniel | 7/22/09 12:29 AM
James Allen's Gravatar Yeah that's what happened to me. The code I had should have worked fine but kept failing with that annoying message. It's at times like that where you start to question your whole code base and start debugging *everything*. Doesn't cross your mind that it might be the actual framework. Totally agree with you - a change as fundamental as that should be VERY clear or have a built in error message if possible.
# Posted By James Allen | 7/22/09 12:33 AM
Jana Fioravanti's Gravatar Hey, thank's a lot man, this was awesome, saved me from lot of issues.
# Posted By Jana Fioravanti | 1/6/10 8:04 PM
Jörg Rostock's Gravatar Many Thank,

the first search Result in search Engine show me this Answer :)
# Posted By Jörg Rostock | 1/15/10 7:16 PM
kiran's Gravatar Thanks. saved lots of my time.
# Posted By kiran | 1/25/10 6:00 PM
Catherine G's Gravatar Thanks! Exactly the fix I was looking for.
# Posted By Catherine G | 2/25/10 3:36 AM
© 2010 James Allen | Contact Me
This blog runs on the awesome power of BlogCFC - created by Raymond Camden. This blog is running version 5.9.