">
" />
Easy Tutorial
❮ Traversing Add Ajax Ajaxstart ❯

jQuery.unique() Method

jQuery Miscellaneous Methods

Example

Remove duplicate div elements from an array

<div>There are 6 div blocks in the document</div>
<div></div>
<div class="dup"></div>
<div class="dup"></div>
<div class="dup"></div>
<div></div>
<script>
$(function () { 
    // unique() gets the original array
    var divs = $( "div" ).get();
    // Add 3 div block elements 
    divs = divs.concat( $( ".dup" ).get() );
    $( "div:eq(1)" ).text( "There are " + divs.length + " elements after sorting." );

    divs = jQuery.unique( divs );
    $( "div:eq(2)" ).text( "There are " + divs.length + " elements after sorting." )
        .css( "color", "red" );
})
</script>

Definition and Usage

The $.unique() function sorts an array of DOM elements, in place, with the duplicates removed.

Note: 1. This only works on arrays of DOM elements, not strings or numbers.

Syntax

Parameter Description
array Array type The specified array of DOM elements.

jQuery Miscellaneous Methods

❮ Traversing Add Ajax Ajaxstart ❯