-1

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.

asked Jul 16, 2010 at 15:51

4 Answers 4

2

try changing

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

with

 $("#myVal").blur(function() {
 ^^^
answered Jul 16, 2010 at 15:55
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Jul 16, 2010 at 15:55

Comments

1

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.

answered Jul 16, 2010 at 16:01

Comments

0

this did the trick :

$().ready(function () {
 var arr = [];
 $("#myVal").blur(function () {
 if ($.inArray($('#myVal').val(), arr) == -1) {
 arr.push($("#myVal").val());
 }
 $("#myArr").val(arr);
 });
});
Ankur
12.8k7 gold badges40 silver badges68 bronze badges
answered Jul 16, 2010 at 16:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.