mcbeav
mcbeav

Reputation: 12275

How can i find an element with a custom attribut that equals a specific value?

I am trying to use a .each loop to find specific elements in a list that when a custom attribute, "pid", is not equal to 0 i can perform an action. How can i go about finding each element? so far i have:

$.each($('.container ul li'),function() {
  var pid = $('.container ul li').attr('pid');
  $(this).remove().after($("container ul li[pid='"+pid+"']"));

  });

but this is not doing anything here. can anyone please help?

essentially what i am trying to do is take each element in the list and group them together by the attribute "pid", so i am searching the list for when the pid does not equal 0, then i need to search it again, and find its match, when each pid is equal to each other, (note: there will only be one match), and append it after the first, or the original.

Upvotes: 2

Views: 565

Answers (1)

Kyle Sletten
Kyle Sletten

Reputation: 5413

This will find any li with a pid that is not 0 if that is what you want. It will then start to group the items by pid.

var groups = [];

$('.container ul li[pid][pid!="0"]').each(function(){
    var pid = this.getAttribute("pid");

    if(groups[pid]){
        groups[pid].push(this);
    }else{
        groups[pid] = [this];
    }
}

This code would produce a list of lists. You can access all of the items with a given pid by accessing groups[pid] so all of the items with pid = 5 will be in groups[5].

Upvotes: 2

Related Questions