Title: Searching an Array with the user input using javascript
Tags: javascript;html;ajax
Question: I want to search a value in the array.
array is predefined with some values.
There will be a HTML text box to enter a value. User need to enter the value.
If the value entered by user is there in array. it should display "Valued found" or else not found. It should use AJAX.
My below code is not working.

```<html&gt;
<head&gt;
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"&gt;</script&gt;
</head&gt;
<body&gt;
<p&gt;Enter a value: <input type="text" id="carInput" onchange="textChanged()"&gt;</p&gt;
<p id="onListText"&gt;</p&gt;
<script&gt;
var cars = ["abc", "def", "ghi","jkl"];
var textChanged = function(){
  var inputText = $("#carInput").text();
  if($.inArray(inputText, cars) !== -1){
    $("#onListText").text("Value Found");
    return;
  }
}
</script&gt;
</body&gt;
</html&gt;
```

Comment: @akxlr may he find local cache first, if not found, then query DB?

Comment: What does this have to do with AJAX?

Comment: $("#carInput").text(); should be $("#carInput").val()


Here is another answer: If you want to trigger the event only on lost focus, you just replace text() to val()

```var inputText = $("#carInput").val(); //replace text() to val()
```

using onkeyup(or oninput, suggest by @mohamedrias) rather than onchange event is more suitable in your case.
onchange need blur to fire the event.

```<p&gt;Enter a value: <input type="text" id="carInput" onkeypress="textChanged()"&gt;</p&gt;
```

onkeyup pnlkr

Comment for this answer: Even better, OP can bind just `oninput` event

Here is another answer: ```$("#carInput").text();``` should be ```$("#carInput").val()```

```text()``` is the select all the text inside the selected element

```val()``` is the get the val of form control


```var cars = ["abc", "def", "ghi", "jkl"];
var textChanged = function() {
  var inputText = $("#carInput").val();
  if ($.inArray(inputText, cars) !== -1) {
    $("#onListText").text("Value Found");
    return;
  } else {
    $("#onListText").text("Value not Found");
  }
}```
```<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;</script&gt;

<p&gt;Enter a value:
  <input type="text" id="carInput" onchange="textChanged()"&gt;
</p&gt;
<p id="onListText"&gt;</p&gt;```





Here is another answer: Try ```var inputText = $("#carInput").val();``` instead of ```var inputText = $("#carInput").text();```

And use ```oninput``` event.



```<html&gt;
<head&gt;
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"&gt;</script&gt;
</head&gt;
<body&gt;
<p&gt;Enter a value: <input type="text" id="carInput" oninput="textChanged()"&gt;</p&gt;
<p id="onListText"&gt;</p&gt;
<script&gt;
var cars = ["abc", "def", "ghi","jkl"];
var textChanged = function(){
  var inputText = $("#carInput").val();
  if($.inArray(inputText, cars) !== -1){
    $("#onListText").text("Value Found");
    return;
  }
}
</script&gt;
</body&gt;```





Here is another answer: ```  var inputText = $("#carInput").text();
```

This should be 

```  var inputText = $("#carInput").val();
```

So your full code will be:



``` var cars = ["abc", "def", "ghi","jkl"];
var textChanged = function(){
  var inputText = $("#carInput").val();
  console.log(inputText);
  if($.inArray(inputText, cars) !== -1){
    $("#onListText").text("Value Found");
    return;
  } else {
    $("#onListText").text("Value not Found");
  }
}```
```  
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"&gt;</script&gt;
<p&gt;Enter a value: <input type="text" id="carInput" oninput="textChanged()"&gt;</p&gt;
<p id="onListText"&gt;</p&gt;```





{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:04/10/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><html><ajax>', 'Title': 'Searching an Array with the user input using javascript', 'ViewCount': 73, 'id': 29553746, 'user_AboutMe': None, 'user_AccountId': 4957263.0, 'user_DisplayName': 'Muneeb K', 'user_Reputation': 415.0, 'user_Views': 59.0, 'user_id': 3989503.0}}