Nuno Silva
Nuno Silva

Reputation: 758

Different results for same jQuery code in chrome and Firefox

I can't figure out what the problem is. The following code works well in Chrome (v29) but not on firefox (v23):

// some test data
var serviceOptions = '<optgroup label="Domiciliary Care"><option value="1">Meals</option><option value="2">Personal Hygiene</option></optgroup><optgroup label="Live in Care"><option value="3">Housekeeping and Cleaning</option><option value="4">Assisted Mobility</option></optgroup>';
var cat = "Live in Care";

// escape illegal characters
var escaped_cat = cat.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, "\\$1");

var options = $(serviceOptions).filter("optgroup[label='" + escaped_cat + "']").html();
var expectedOptions = '<option value="3">Housekeeping and Cleaning</option><option value="4">Assisted Mobility</option>';
// OK in Chrome, ERROR in FF
console.log(options == expectedOptions ? "OK" : "ERROR")

// passing the string directly instead of using a var works on FF
options = $(serviceOptions).filter("optgroup[label='Live\ in\ Care']").html();
console.log(options == expectedOptions ? "OK" : "ERROR")

The goal is simple as you can see: to filter a set of options by a specific optgroup. You can test it by opening the jQuery website, opening the console and copy-paste the code. Meanwhile if I discover something, I'll update this post.

UPDATE Thanks for the feedback: It seams that the problem is with the escaping of illegal characters, on firefox with jQuery 1.9.1. Either upgrading jQuery or not escaping the characters produces the expected results under Firefox.

Since upgrading jQuery is not an option for me, anyone knows what can be the problem with that escape regex?

Upvotes: 1

Views: 350

Answers (1)

DGS
DGS

Reputation: 6025

Seems to be an issue with older versions of jQuery but not newer ones. Try updating your jQuery to 1.10

Not working old version of jQuery including migrate 1.9.1 JSFIDDLE

Working new version of jQuery 1.10.1 JSFIDDLE

the jQuery main site is running an older version of jQuery by the way

Upvotes: 4

Related Questions