Reputation: 4044
Here's my code so far:
$('#floorplans img').each(function() {
$('#floorplans a').each(function() {
$(this).attr('rel','lightbox');
});
});
It works well so far, but I need it to do same thing for #gallery
too. Is it possible to accomplish this without having to write the same code again for #gallery
?
Thank you!
Upvotes: 1
Views: 2693
Reputation: 1221
Would
$('#floorplans img a, #gallery img a').attr('rel', 'lightbox');
not do?
Update: I had "IMG" and "A" back to front, should have been all "A" which contain "IMG" inside "#Div", like this:
$('#floorplans a:has(img), #gallery a:has(img)').attr('rel', 'lightbox')
Upvotes: 2
Reputation: 2096
Without having seen your markup, something like this may work:
$('#floorplans img, #gallery img').each(function() {
$('a').each(function() {
$(this).attr('rel','lightbox');
});
});
HTH
Update: I still don't think this is right, you aren't using the elements youve selected in the outer loop so its redundant. And I think you are setting the rel attr multiple times because of it. I havent tried it but I think you should only be using one loop, where you select links that have an image.
$('#floorplans a:has(img), #gallery a:has(img)').each(function() {
$(this).attr('rel','lightbox');
});
Upvotes: 1
Reputation: 2534
You can define function and use it, like:
var addRel = function(selector) {
$(selector).each(function() {
$(this).attr('rel','lightbox');
});
};
addRel('#floorplans a');
addRel('#gallery a');
Upvotes: 0