8

I have a table. Some rows are dynamically added by jquery.

The first <td> of each row has an <input type="text" /> element. Using jQuery, is it possible to check that all these input elements have unique values?

5
  • No, your english is just meh. Commented Mar 10, 2010 at 17:38
  • @hobodave: Thanks. I know, but sometimes what can I do? I'm trying. :) Commented Mar 10, 2010 at 17:40
  • I'm not criticizing, just explaining. Commented Mar 10, 2010 at 17:41
  • @lowiji, how about confirming an answer? Commented Mar 12, 2010 at 14:50
  • @st. Woland - answer confirmed :) Commented Mar 13, 2010 at 10:11

2 Answers 2

7

Nick's solution has O(n2) complexity. Here's an optimized example.

Function isUnique determines the required result.

<script src="jquery.js" />
<script>
function isUnique( tableSelector ) {
    // Collect all values in an array
    var values = [] ;
    $( tableSelector + ' td:first-child input[type="text"]' ).each( function(idx,val){ values.push($(val).val()); } );

    // Sort it
    values.sort() ;

    // Check whether there are two equal values next to each other
    for( var k = 1; k < values.length; ++k ) {
        if( values[k] == values[k-1] ) return false ;
    }
    return true ;
}

// Test it
$(document).ready(function(){
    alert( isUnique( ".myTable" ) ) ;
});
</script>

<table class="myTable">
    <tr><td><input type="text" value="1" /></td></tr>
    <tr><td><input type="text" value="2" /></td></tr>
</table>
Sign up to request clarification or add additional context in comments.

6 Comments

This loops through the array 3 times plus a sort...the performance difference would take a hundred thousand loops to make 1ms, all your time is spent in the selector in this case. For things like this I'll take more straight-forward code than .000001ms improvements in execution time any day of the week :)
I'd consider having two options, one for every possible scenario (of course, your solution works much better for a long array with equal values in the beginning; mine will be faster, if they are in the end). Besides, there are idealists, who live to produce a better-looking code ;)
Agreed, depends where the duplicates are...I suppose I'm not in the habit of making pages large enough that the performance would have more than a .0000001ms difference, so I opt for the cleanest code approach...or rather what seems the cleanest to me.
Thanks, for your answers. But I find it difficult to choose the best answer.
@loviji - This one, St deserves rep for the analysis, and I'm capped for the day :)
|
7

You can use an array for this and the jQuery .inArray function like this:

var vals = new Array();
$("td:first-child input").each(function() {
  if($.inArray($(this).val(), vals) == -1) { //Not found
     vals.push($(this).val());
  } else {
    alert("Duplicate found: " + $(this).val());
  }      
});

Be sure to clear vals before a second pass if you're reusing it.

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.