Jump to content

My jQuery array items only index 0


anevins

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/265128-my-jquery-array-items-only-index-0/
Share on other sites

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.

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());
  }
});

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.