Making a variable in jquery 1.9.1 fails and in 1.8.3 doesn't
Asked Answered
V

2

2

I just upgraded to 1.9.1 After some technical defiificulties all work except:

var $newthumbs = $('                <div id=\"car-7\" class=\"thumbnail_car thumbnail span2\" data-sorting-top=\"2\" data-sorting-date=\"2013-01-12 16:47:31 UTC\"></div>');

If I put this line in the console of jquery 1.8.3 it gets accepted and I can retrieve it with $newthumbs

in 1.9.1 it fails with Error: Syntax error, unrecognized expression:

I've read the changelog and I don't see anything relating that should break this. I may not know a lot of jquery, but this type of syntax looks standard.

What changed?

Update

I did find this here

HTML strings with leading whitespace: jQuery 1.9 restricts the strings processed by $() for security reasons. Although we recommend you use $.parseHTML() to process arbitrary HTML like templates, the 1.1.0 version of the Migrate plugin restores the old behavior.

Vocalist answered 11/2, 2013 at 17:3 Comment(2)
why do you wrap that in $? If it's just a regular string you can just remove it.Renae
Remove the gigantor space at the beginning, and it should work fine. $.trim() is your friend.Bemis
C
2

If you remove the spaces, it will work:

var $newthumbs = $('<div id=\"car-7\" class=\"thumbnail_car thumbnail span2\" data-sorting-top=\"2\" data-sorting-date=\"2013-01-12 16:47:31 UTC\"></div>');

DEMO: http://jsfiddle.net/dirtyd77/KFmMQ/1/

Cuzco answered 11/2, 2013 at 17:10 Comment(3)
Might be a trimming problem?Cuzco
so jquery 1.9.1 doesn't allow spaces?Vocalist
Hmm, that seems to be the case... for the time being, you can use a custom trim() function. I have provided you with one in this demo. Wish I could be more helpful! Let me know if you have any questions! Demo: jsfiddle.net/dirtyd77/KFmMQ/2Cuzco
A
0

Your problem are the whitespaces in the beginning of the string. I have no idea why this is a problem, but it obviously is.

By the way: You can remove the pointless backslashes:

var $newthumbs = $('<div id="car-7" class="thumbnail_car thumbnail span2" data-sorting-top="2" data-sorting-date="2013-01-12 16:47:31 UTC"></div>');

or you can use double quotation marks for the surrounding string:

var $newthumbs = $("<div id=\"car-7\" class=\"thumbnail_car thumbnail span2\" data-sorting-top=\"2\" data-sorting-date=\"2013-01-12 16:47:31 UTC\"></div>");
Agaric answered 11/2, 2013 at 17:11 Comment(2)
the thing is that these divs are automatically generated, so baskslash is the rails javascript escapingVocalist
then use the second option.Agaric

© 2022 - 2024 — McMap. All rights reserved.