0

Im new to web dev and jQuery. I have input element binded with blur event.

This is my code:

// this are my input elements:
<input class="input_name" value="bert" />
<input class="input_name" value="king kong" />
<input class="input_name" value="john" />
<input class="input_name" value="john" />

<script>

    $(".input_name").bind("blur",function(){
          alert(findDuplicate($(this).val()));
    })

    function findDuplicate(value){
          var result = 0;
          $(".input_name").each(function{
                 if($(this).val == value){
                        result++;
                 }
          });
    }

</script>

my main problem is when i change bert to john it returns me 3 result. how would i exempt the event sender from being checked?

8
  • 1
    Couldn't you just subtract 1? Commented Sep 30, 2013 at 15:16
  • You have a couple of syntax errors in your code like $(this).val should be $(this).val() and function on each should have () at the end of function Commented Sep 30, 2013 at 15:17
  • @Anton The anonymous function passed to each does not need parentheses at the end. Commented Sep 30, 2013 at 15:18
  • simple solution :) var result = -1; Commented Sep 30, 2013 at 15:18
  • @Asad I agree, I would just set result to -1 initially, though. You'll get a match on itself, taking it up to 0, then you see any actual duplicates in positive results. Commented Sep 30, 2013 at 15:19

3 Answers 3

2

Like others have mentioned, you've got a few syntax errors. Also, rather than explicitly iterating over all the inputs, you could just have jQuery find them for you using selectors:

$(".input_name").bind("blur",function(){
    alert(findDuplicate($(this).val()));
})

function findDuplicate(value){
    return $(".input_name[value='" + value + "']").length - 1;
}
Sign up to request clarification or add additional context in comments.

6 Comments

That won't really work, since value attribute stays the same jsfiddle.net/Alfie/5sYQ3/1
@Anton What do you mean? That fiddle works for me as expected. OP didn't state that the values would change, so this should suffice.
That's true if the values don't change then it's all fine
@Anton Right, and given what we know about the problem, I think that's a valid solution.
then how could i change the background of other 2 input? btw thanks for the answer i think i need to explor jquery selector
|
1
$(".input_name").bind("blur", function () {
    alert(findDuplicate(this.value));
})

function findDuplicate(value) {
    var result = 0;
    $(".input_name").each(function(){
        if (this.value == value) {
            result++;
        }
    });
    return result - 1;
}

DEMO

Comments

0

Try this (untested):

$(".input_name").bind("blur",function(){
    var nth = $(this).index();
    alert(findDuplicate($(this).val(),nth));
})

function findDuplicate(value,nth){
      var result = 0;
      $(".input_name").each(function{
             if($(this).val == value && nth != index){
                    result++;
             }
      });
      return result;
}

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.