I have a bunch of list items with an attribute. I want to select only two or three of them at a time. I am currently split with a comma-separated list of identifiers, and then creating an array of selectors to return jQuery:
var ids = $(this).text().split(','); //eg text = 1,13,27 var selectors = []; for (var index in ids) { selectors.push('.container ul.class li[data-id="'+ids[index]+'"]'); } $(selectors.join(',')).addClass('active');
This is a pretty expensive approach for Firefox. Is there a way to optimize this, so I can select any li with a data-id attribute containing any of the identifiers?
Something similar to the one below, but choose any of the values?
var ids = $(this).text().split(','); //eg text = 1,13,27 $('.container ul.class li[data-id~="' + ids + '"]').addClass('active');
[edit]
Thanks to the comment by @Alnitak, I changed my loops to:
var ids= $(this).text().split(','); var length = ids.length; for (var i=0; i<length;i++) { selectors.push('.container ul.class li[data-id="'+ids[i]+'"]'); }
This has improved significantly, but is there more to be done?
source share