Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting text from <li>´s & check duplicates & .append it... by jQuery

HTML :

<li>Tue, 15th May 2012 10:11</li>
<li>Tue, 15th May 2012 10:12</li>
<li>Tue, 15th May 2012 10:13</li>
<li>Tue, 15th May 2012 10:14</li>
<li>Tue, 15th May 2012 10:15</li>
<li>Tue, 16th May 2012 21:08</li>
<li>Tue, 16th May 2012 21:07</li>
<li>Tue, 16th May 2012 21:06</li>
<code></code>

jQuery :

$("li").each(function () {
    words = $(this).text().split(' ');
    xxx = words[1]+" "+words[2]+" "+words[3]+",";
    $("code").append(xxx); 
});

I come to this step now, but i dont know how to check duplicates date and append unique date

Demo : http://jsfiddle.net/4kvUy/

like image 886
l2aelba Avatar asked May 15 '12 11:05

l2aelba


1 Answers

Do this way:-

if ($("code:contains('"+xxx+"')").length == 0)
   $("code").append(xxx);

EDIT:

To handle Comma, try this way:-

if ($("code:contains('"+xxx+"')").length == 0){
    if ($("code").text() == '')
        $("code").append(xxx);
    else
        $("code").append(","+xxx);
}

EDIT2:

To handle this type of output, try this way:-

$("li").each(function () {
    words = $(this).text().split(' ');
    xxx = words[1]+" "+words[2]+" "+words[3];
    if ($("code:contains('"+xxx+"')").length == 0){     
        if ($("code").text() == '')         
            $("code").append('["'+xxx);     
        else         
            $("code").append('","'+xxx); 
    }
});
$("code").append('"]');

Output:

["15th May 2012","16th May 2012"]
like image 180
Siva Charan Avatar answered Sep 28 '22 04:09

Siva Charan