Nick Ginanto
Nick Ginanto

Reputation: 32120

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

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.

Upvotes: 2

Views: 1184

Answers (2)

yankee
yankee

Reputation: 40750

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>");

Upvotes: 0

Dom
Dom

Reputation: 40461

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/

Upvotes: 2

Related Questions