anevins Posted July 2, 2012 Share Posted July 2, 2012 It seems all my items in 'titlesArray' are all at 0 index $('.main ul li h3').each(function(){ var titles = $(this); var titlesArray = jQuery.makeArray(titles); $.each(titlesArray, function(index, value){ document.write(index); }); }); document.write(index); outputs "00000" There are 5 objects within that array, here is the html code <ul> <li> <h3>Viral Advert</h3> </li> <li> <h3>bannar</h3> </li> <li> <h3>movie</h3> </li> <li> <h3>photo</h3> </li> <li> <h3>movie</h3> </li> </ul> Shouldn't my array indexes be 1,2,3,4,5? Quote Link to comment https://forums.phpfreaks.com/topic/265128-my-jquery-array-items-only-index-0/ Share on other sites More sharing options...
requinix Posted July 2, 2012 Share Posted July 2, 2012 titles is just one element. Because you're inside an each(). $('.main ul li h3').each(function(index, value){ document.write(index); }); Quote Link to comment https://forums.phpfreaks.com/topic/265128-my-jquery-array-items-only-index-0/#findComment-1358673 Share on other sites More sharing options...
anevins Posted July 2, 2012 Author Share Posted July 2, 2012 Can you see my intentions of a foreach iteration and help me construct jQuery/ javascript for this? Quote Link to comment https://forums.phpfreaks.com/topic/265128-my-jquery-array-items-only-index-0/#findComment-1358678 Share on other sites More sharing options...
haku Posted July 2, 2012 Share Posted July 2, 2012 What are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/265128-my-jquery-array-items-only-index-0/#findComment-1358682 Share on other sites More sharing options...
anevins Posted July 2, 2012 Author Share Posted July 2, 2012 I want to check for a duplicate of a value $('.main ul li h3').each(function(){ var titles = $(this); var titlesArray = jQuery.makeArray(titles); jQuery.each(titlesArray, function(index, value){ if (jQuery.inArray(value, titlesArray)){ } }); }); Then to hide, not remove, the 2nd or more list item that has the duplicate. Quote Link to comment https://forums.phpfreaks.com/topic/265128-my-jquery-array-items-only-index-0/#findComment-1358684 Share on other sites More sharing options...
haku Posted July 2, 2012 Share Posted July 2, 2012 Try this: var foundValues = []; $('.main ul li h3').each(function() { if($.inArray($(this).val(), foundValues)) { $(this).hide(); } else { foundValues.push($(this).val()); } }); Quote Link to comment https://forums.phpfreaks.com/topic/265128-my-jquery-array-items-only-index-0/#findComment-1358694 Share on other sites More sharing options...
anevins Posted July 2, 2012 Author Share Posted July 2, 2012 That works to the extent of hiding the <h3>, which is the title. Sorry to be unclear. I want to hide the <li>, however that code hides all <li> Quote Link to comment https://forums.phpfreaks.com/topic/265128-my-jquery-array-items-only-index-0/#findComment-1358701 Share on other sites More sharing options...
anevins Posted July 3, 2012 Author Share Posted July 3, 2012 I can't find a way to select the outer element, containing the <h3>, to hide instead Quote Link to comment https://forums.phpfreaks.com/topic/265128-my-jquery-array-items-only-index-0/#findComment-1358716 Share on other sites More sharing options...
haku Posted July 3, 2012 Share Posted July 3, 2012 var foundValues = []; $('.main ul li h3').each(function() { if($.inArray($(this).text(), foundValues)) { $(this).hide(); } else { foundValues.push($(this).text()); } }); Quote Link to comment https://forums.phpfreaks.com/topic/265128-my-jquery-array-items-only-index-0/#findComment-1358717 Share on other sites More sharing options...
anevins Posted July 3, 2012 Author Share Posted July 3, 2012 Ah that's having the same problem as before, it's hiding all of the <li> Quote Link to comment https://forums.phpfreaks.com/topic/265128-my-jquery-array-items-only-index-0/#findComment-1358718 Share on other sites More sharing options...
haku Posted July 3, 2012 Share Posted July 3, 2012 Did you copy it exactly as is? Because it's impossible that it is hiding any of the li tags. Anyways, I forgot one of the changes: var foundValues = []; $('.main ul li h3').each(function() { if($.inArray($(this).text(), foundValues) !== -1) { $(this).parent().hide(); } else { foundValues.push($(this).text()); } }); Quote Link to comment https://forums.phpfreaks.com/topic/265128-my-jquery-array-items-only-index-0/#findComment-1358720 Share on other sites More sharing options...
anevins Posted July 3, 2012 Author Share Posted July 3, 2012 I had copied it exact, but the latest reply code does work and I've appreciated your help. Quote Link to comment https://forums.phpfreaks.com/topic/265128-my-jquery-array-items-only-index-0/#findComment-1358722 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.