I’m currently working on my Stretchbox Plugin and trying to optimize and shorten the code.
I’ve used the jquery data method to attach data to certain divs.
For instance:
$('#thumbs div:last-child').data('active', true)
which sets a certain div to the active state.
If i know want to find this div, i have to check each .thumb class
in order to find it:
$('.thumb').each(function() {
if($(this).data('active')){
//Do Stuff
}
}
This works fine, but I’m quite sure there should be a much easier way, since checking up every single .thumb div(out of 10-30) will take some performance too.
As far as I know there is no other way to do it. You could, however, create a new jQuery selector. I was going to give it a shot, but it looks like someone has already thought of it (scroll down to “Querying element data”).
It will allow you to do things like this:
It probably won’t be faster, but it might make your code neater!