3

I have list like this.

<li data-pet="cat"></li>
<li data-pet="cat"></li>
<li data-pet="cat"></li>
<li data-pet="dog"></li>

Using jQuery how can I grab the value of the data-attributes to count if the same value appears more than once.

I then want to append a span to first list element with that value.

Result should look like this.

<li data-pet="cat"><span class="first"></span></li>
<li data-pet="cat"></li>
<li data-pet="cat"></li>
<li data-pet="dog"></li>

Thanks for your help.

0

4 Answers 4

3

a possible implementation

(function() {  
    var pets = {};
    $('li[data-pet]').each(function() {
        var li = $(this),
            pet = li.data('pet');

        if (!pets[pet]) {  
            pets[pet] = { firstnode: li, count : 1 }
        }
        else {
            pets[pet]['count'] = pets[pet]['count'] + 1;
        }
    });

    $.map(pets, function(obj) {     
        if (obj.count > 1) {
          obj.firstnode.append('<span class="first">'+ obj.count +'</span>');
        }
    });
}());

html

<li data-pet="cat">cat</li>
<li data-pet="cat">cat</li>
<li data-pet="cat">cat</li>
<li data-pet="dog">dog</li>
<li data-pet="elephant">elephant</li>
<li data-pet="elephant">elephant</li>

Example fiddle: http://jsfiddle.net/HJ6gF/6/

Sign up to request clarification or add additional context in comments.

2 Comments

Excellent. Including the total was nice touch. I will also that aswell. Thanks.
I changed a bit the implementation so the code is more performant - no real need to read a selection of li nodes inside $.map function.
0

A way for this would be (you should optimize/neaten it according to your situation ofcourse):

var selector = "cat";
var selectorCount = 0;

$("li[data-pet="+selector+"]").each(function() {
    selectorCount++;
});

if(selectorCount > 1) {
    $("li[data-pet="+selector+"]").each(function() {
        $(this).html("<span class=\"first\"></span>");
        break;
    });
}

3 Comments

Why do you need a .each when you just .length on selector?
Thanks for your answer. Two problems. First there is syntax error and also why variable called "cat". I want get all data-attributed regardless of value and then count them to determine if there is more than once occurrence of it.
@jamjam: Hence the "you should optimize/neaten it according to your situation". You aren't providing any context information on your issue so I was unable to tailor the code to your needs. You should be able to deduct what you need to change from this code...
0
var tarr = [];
$('li').each(function() {
   tarr.push($(this).attr('data-pet'))
});
var count = $.unique(tarr).length;

1 Comment

From the jQuery API Docs: "Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers."
0
<li data-pet="cat"/>
<li data-pet="cat"/>
<li data-pet="cat"/>
<li data-pet="dog"/>

<script type="text/javascript">

    var dataPets = [];

    $('li').each( function( index )
    {
        if ( $.inArray( $(this).attr( "data-pet" ) , dataPets ) )
        {
            $(this).append( "First" );
        }

        dataPets[index] = $(this).attr( "data-pet" );
    });

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.