Skip to content

Instantly share code, notes, and snippets.

@NateJLewis
Created July 9, 2015 20:30
Show Gist options
  • Save NateJLewis/cb8889a9c01d2840fd2a to your computer and use it in GitHub Desktop.
Save NateJLewis/cb8889a9c01d2840fd2a to your computer and use it in GitHub Desktop.
jQuery-selection-iteration-issue
// ok here is my issue...
// I am trying to add hidden to all the .product-image div's then add .active to the first one
// each set is nested in a tr td...
// ALL the .product-image end up with .active
// when I do this -->
$(document).ready(function() {
var productImg = $('.product-image'),
campaignRow = $('.campaign-row'),
oddRowProductImg = $('tr.odd .product-image'),
evenRowProductImg = $('tr.even .product-image');
if(!productImg.hasClass('hidden')){
productImg.addClass('hidden');
}
if(productImg.first()){
productImg.removeClass('hidden').addClass('active');
}
});
// ======================================================
// All .product-image are .hidden
// when I do this -->
var productImg = $('.product-image'),
campaignRow = $('.campaign-row'),
oddRowProductImg = $('tr.odd .product-image'),
evenRowProductImg = $('tr.even .product-image');
oddRowProductImg.not('.hidden').addClass('hidden');
if(oddRowProductImg.first().not('.active')) {
oddRowProductImg.first().removeClass('hidden').addClass('active');
}
evenRowProductImg.not('.hidden').addClass('hidden');
if(evenRowProductImg.first().not('.active')) {
evenRowProductImg.first().removeClass('hidden').addClass('active');
}
// ======================================================
// the first one is correct the other 2 rows are all hidden
// when I do this -->
var campaignRow = $('.campaign-row'),
campaignRowProductImg = $('.campaign-row .product-image'),
productImg = $('.product-image'),
campaignRowProductImg.addClass('hidden');
campaignRowProductImg.first().removeClass('hidden').addClass('active');
// ======================================================
// I get mad errors
// when I do this -->
$( campaignRow ).each( function( index, el ) {
var productImg = $('.product-image');
$( el ).productImg.addClass('hidden');
$( el ).productImg.first().removeClass('hidden').addClass('active');
});
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment