0

I have one textbox and one button and on button I have written below code. problem is suppose first I have entered in textbox 10 than its worked but when another time I enter 10 than also it prints value is not in array. so pls help me whats the issue...

jQuery(document).ready(function()
{
 jQuery("#mybutton").live('click',function () 
 {
    var sel_fam_rel=jQuery("#my_textbox").val();
    var ids = [];
    code =sel_fam_rel;
    if($.inArray(code,ids) >= 0)
    {
      alert("Value is in array");
    }
    else
    {
      alert("Value is not in array");
      ids.push(code);
    }
  });
});

3
  • 2
    You are re-initializing ids, So it will always go to else section. Show us your compete JS code. Commented Mar 25, 2015 at 6:25
  • 2
    if($.inArray(code,ids) != -1) isn't this would be there? Commented Mar 25, 2015 at 6:28
  • i have added but still same issue getting. jai Commented Mar 25, 2015 at 6:32

5 Answers 5

2

This line:

if($.inArray(code,ids) >= 0)

should be changed to:

if($.inArray(code,ids) != -1)

and put your ids var outside of click.

Try the snippet below.

var ids = [];
jQuery("button").on('click', function() {
  var sel_fam_rel = jQuery("#my_textbox").val();

  code = sel_fam_rel;
  if ($.inArray(code, ids) != -1) {
    alert("Value is in array");
  } else {
    alert("Value is not in array");
    ids.push(code);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' id='my_textbox'>
<button>check</button>

Sign up to request clarification or add additional context in comments.

4 Comments

What difference does it make if it is >=0 or !=-1?
@charlietfl !=-1 , seems to better convey the intent to me, but good point
@DelightedD0D that is purely subjective. The only difference here that matters is storing the array outside the click handler
Should the OP not also be encouraged to use on() or .delegate() over live()?
1

Create your array var ids=[];global outside button event, as whenever you click button it is creating new empty array. It will fix your problem.

Comments

1

A few changes are needed:

var ids = []; // `ids` needs to be in the global scope to work as you want it, 
              //  or you could use a different method like localstorage 
jQuery(document).ready(function()
{
 jQuery("#mybutton").on('click',function () // use `on` not `live` which is deprecated
 {
    var sel_fam_rel=jQuery("#my_textbox").val();
    code =sel_fam_rel;
    if($.inArray(code,ids) != -1)  // inArray() returns -1 if the value is not in the array, you can use it the way you have it, IMO (purely subjective), using `!=-1` is preferable as it's more clear what the code in intend to do
    {
      alert("Value is in array");
    }
    else
    {
      alert("Value is not in array");
      ids.push(code);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="my_textbox" value="10"/><br>
<input type="button" id="mybutton" value="Click me"/>

Comments

1

use below code . take your ids out side of click event . as per your code each time when you click button ids reset .

var ids = [];  // declare as global variable
jQuery(document).ready(function()
{
  jQuery("#mybutton").live('click',function () 
  {
    var sel_fam_rel=jQuery("#my_textbox").val();
    code =sel_fam_rel;
    if($.inArray(code,ids) >= 0)
    {
     alert("Value is in array");
    }
   else
   {
     alert("Value is not in array");
     ids.push(code);
    }
 });
});

3 Comments

shouldn't the OP be encouraged to use on() over live()?
thanks... in above code if i change this line of code if($.inArray(code,ids) != -1) instead of if($.inArray(code,ids) >= 0) than only it will be worked..
@MahendraPumbhadiya its not like you change it >= 0 to != -1 this both condition is same. it work because you put ids = [] out side of click event .
-1

I made a fiddle to your problem, Use indexOf

http://jsfiddle.net/go8o34fq/

jQuery-

var array=["A","B","C","D"];

$('button').click(function(){
    var code=$('input').val();
    if(array.indexOf(code)==-1)
    {
        array.push(code);
       console.log("if "+array)
    }
    else
    {
      console.log("else "+array)
    }
});

Just a bit requirement if your need is case-sensitive then use- code.toUpperCase()

1 Comment

-1 because you tell the OP to use indexOf (which is not really the issue). and while you do change the scope of the array (which was the problem), you dont mention that in your answer

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.