tags:

views:

29

answers:

5

I have the following :

<script>
$().ready(function() {

 $("#myVal#").blur(function() {
   var arr = jQuery.makeArray(  $("#myArr").val() )
if ( $("#myVal").val().indexOf(arr) == -1 || $("#myVal").val().indexOf(arr) ==0) {
    arr.push($("#myVal").val());
}
$("#myArr").val(arr)
});
});
</script>
<form action="" method="post" name="myF" id="myF">
 <input type="text" name="myVal" id="myVal" value="">
 <input type="text" name="myArr" id="myArr" value="">
 <br/>
<input type="submit" name="submit" id="submit" value="go">
</form>

I am trying to check and see if a particular value entered to myVal is already in myArr. If so, don't add to the array. If not, then add to the array. However, the array keeps growing with duplicate values.

what am i doing wrong?

thanks in advance.

+2  A: 

try changing

 $("#myVal#").blur(function() {
         ^^^

with

 $("#myVal").blur(function() {
         ^^^
jigfox
i guess i need some coffee. thanks
FALCONSEYE
+1  A: 

First, makeArray is meant to take array-like objects as input, not strings. And I don't see the purpose here. If you have an actual array arr (perhaps made using split), you can do:

if ( $.inArray($("#myVal").val(), arr) == -1)

Your current code calls String.indexOf(arr), so arr is implicitly converted to a (comma-separated)string. Then you search for the array string in the new value. This is clearly not what you want.

Matthew Flaschen
A: 

use .inArray() to detect if a specific value is in an array.

if($.inArray($('#myVal').val(), arr))
  alert('yay');

You can't use .makeArray() like you do in your example. .makeArray() generates an array out of a array like object, thus , it needs an objects as parameter.

Reference: .inArray()

jAndy
the problem is like : which one came first, chicken or egg? if i follow your example: var arr = [];$("#myVal").blur(function() { if($.inArray($('#myVal').val(), arr)) { arr.push($("#myVal").val()); } $("#myArr").val(arr);});then, i keep adding dups into the array.
FALCONSEYE
also, you are missing a ')'
FALCONSEYE
@FALCONSEYE - This answer isn't correct...you need to check if `$.inArray(value, array) == -1` to see if it's not found...it's not a boolean result, but rather the *position* in the array the element's found at.
Nick Craver
@Nick: where is the difference? `if($.inArray(val, array))` will return `false` if no position was found. Any return value zero or below will return false.
jAndy
@jAndy - That's incorrect, the first element will return position 0, `-1` is not found. Example: `$.inArray('foo', ['foo', 'bar']) === 0`, test it here: http://jsfiddle.net/yMjvd/ String/Array functions are like this in every language I can think of, no difference in JavaScript.
Nick Craver
@Nick: So my statement was pretty much correct. `Any return value zero or below will return false`. Just lost the zero index from my focus.
jAndy
@jAndy - The second half was correct yes, but the important part "`if($.inArray(val, array))` will return `false`" is not, and the most common mis-use of `$.inArray()` or `.indexOf()`.
Nick Craver
+1  A: 

I believe what you're after is something like this using $.inArray():

$(function() {
 $("#myVal").blur(function() {
   var arr = $("#myArr").val().split(',');
   if ($.inArray($("#myVal").val(), arr) == -1) arr.push($("#myVal").val());
   $("#myArr").val(arr.join(','));
 });
});​

Try a demo here, now this would break if the value had a comma...just choose an appropriate delimiter. What this is going is taking the string out, calling .split() to turn it into an array, splitting the items at the provided delimiter, adding the value if it's missing ($.inArray() returns the postion of the element, -1 if it's not found). Then we're just calling .join() with the same delimiter to turn it back into a string.

Nick Craver
Nick, very sweet!
FALCONSEYE
A: 

this did the trick :

    $().ready(function() {
             var arr = [];
           $("#myVal").blur(function() {
 if( $.inArray($('#myVal').val(), arr) == -1) {
  arr.push($("#myVal").val());
 }
    $("#myArr").val(arr);
});
});

thanks!

FALCONSEYE