html - Searching an Array with the user input using javascript -
html - Searching an Array with the user input using javascript -
i want search value in array. array predefined values. there html text box come in value. user need come in value. if value entered user there in array. should display "valued found" or else not found. should utilize ajax. below code not working.
<html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> </head> <body> <p>enter value: <input type="text" id="carinput" onchange="textchanged()"></p> <p id="onlisttext"></p> <script> var cars = ["abc", "def", "ghi","jkl"]; var textchanged = function(){ var inputtext = $("#carinput").text(); if($.inarray(inputtext, cars) !== -1){ $("#onlisttext").text("value found"); return; } } </script> </body> </html>
var inputtext = $("#carinput").text();
this should
var inputtext = $("#carinput").val();
so total code be:
class="snippet-code-js lang-js prettyprint-override"> 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"); } }
class="snippet-code-html lang-html prettyprint-override"> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <p>enter value: <input type="text" id="carinput" oninput="textchanged()"></p> <p id="onlisttext"></p>
javascript html ajax
Comments
Post a Comment