8

I can't seem to find a definite answer for this. Consider the following:

var dupe = false;
    $(".syndUID").sort(function(a,b) {
        return a - b;
    }).each(function(i, el) {
        alert($(this).val() + " - " + $(this).next().val());
        if($(this).val() == $(this).next().val()) {
                dupe = true;
                return;
        };
    });

This code is an attempt to find duplicate values in a set of inputs with the class syndUID. They are scattered about a form, so not next to eachother in the DOM.

next().val() is always undefined though. am I using the wrong function? How do I simply peek ahead to the next element? I have access to the index, but I don't know how to even make use of it.

EDIT:

After reading the comments and answers I realized there really is no proper iterator in jQuery, which seems really stupid to me since it provides each(). I also had another bug with the above code. Here is the final solution I used that works:

// duplicate check
    var dupe = false;
    var prevVal = undefined;
    $(".syndUID").sort(function(a,b) {
        return $(a).val() - $(b).val();
    }).each(function() {
        if($(this).val() == prevVal) {
            dupe = true;
            return false;
        }
        prevVal = $(this).val();
    });

For anyone who finds this via google, the answers provided by others may be a decent alternative solution, but for my needs I found this sufficed.

dmarra
  • 853
  • 8
  • 23
  • 3
    `next()` will get the next sibling element, not necessarily the next element in the matched set. – Rory McCrossan Dec 22 '15 at 17:14
  • So we get to this question: http://stackoverflow.com/questions/3670136/jquery-find-next-element-by-class. – nicael Dec 22 '15 at 17:16
  • 1
    You only want to know if there are duplicate values? At least that's what the code implies (for me) – Andreas Dec 22 '15 at 17:24
  • What exactly are you trying to do? there are many options, as well as many unknowns for us. Can there be more than 1 duplicate, are you just trying to warn the user if there is a duplicate? – Adjit Dec 22 '15 at 17:39

3 Answers3

3

You can do something like $('.syndUID')[i+1] to regrab the list, focusing on that element (and optionally turning it back into a jQuery object)

Using [i+1] will return a DOM element, but you can use .eq(i+1) to return a jQuery object. Or if you hate your future developers, wrap the DOM element in $() because screw readability!

As Andreas stated -- regrabbing the list is wasting memory. Before the loop, cache the object into a variable with var element = $('.syndUID') so you're not iterating the DOM so much.

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
  • So for each element in `$('.syndUID')` you're going to fetch all the elements with class `syndUID` again? O.o `var elements = $('.syndUID'); elements.each(function(i, el) { var nextElement = elements.eq(i + 1); ... }` – Andreas Dec 22 '15 at 17:19
  • 1
    That is an kinda a clunky method to keep grabbing the same list. Set it to a var, and then apply the indexing to the variable so that you keep the list cached - adding or removing from the list as needed. – Korgrue Dec 22 '15 at 17:28
-1

next() grabs the next sibling - not the next ancestor or parent.

For example, if we have an unordered list, next() works like this:

var a = $('#listone.first').next().val();
console.log(a) //outputs "Second"

<ul id="listone">
<li class="first">First</li>
<li class="second">Second</li>
</ul>
<ul id="listtwo">
<li class="third">Third</li>
<li class="fourth">Forth</li>
</ul>

So, if you are trying to grab the next parent, .val() applied to .next() wont work. I dont know what your outputting to (table, UL, DIV, etc) so it is hard to give you a working solution - but that is likely your problem. You are not traversing the DOM properly.

Korgrue
  • 3,430
  • 1
  • 13
  • 20
  • A valid observation however this doesn't actually answer the question. – j08691 Dec 22 '15 at 17:35
  • Yes, I know. I need to know the structure of what he/she is trying to grab values from so that I can edit my answer with a solution to his/her problem. I am just pointing out the problem with his approach to grabbing element values. This should probably be a comment, not an answer. – Korgrue Dec 22 '15 at 17:48
-1

Well, you have your sorted array already, maybe instead of trying to do this with .each() you just use a simple for loop?

var elemArray = $(".syndUID").sort(function(a,b) {
    return a - b;
});

for(var i = 0; i < elemArray.length; i++){
    if(elemeArray[i].value == elemArray[i+1].value){
       //do something with comparison
    }
}

However, this will only check for a duplicate in the next syndUID element in the array. In order to do a complete search you would need to check each element against every element (excluding itself) from the array. That would involve a nested loop which will add an order of n^2 to your function

Adjit
  • 10,134
  • 12
  • 53
  • 98
  • I wrote exactly that, except you (for some reason) omitted the alert OP had put in place. You're not even setting the `dupe` flag. –  Dec 22 '15 at 17:31
  • @JᴀʏMᴇᴇ This site is not to completely re-write code. Just trying to point the OP in the right direction. – Adjit Dec 22 '15 at 17:32