Is there a way to check if an user input from text box (for example) exists in an array that I created and imported from other JS files?
And can I link all that code to an HTML button?
In Python to do this I use "in":
if a in List:
print("That's In")
PS: I know that List in Python are a little different with Arrays in JavaScript.
From my platform's JavaScript documentation:
//Use:
if (in_array('a', ['a','b',4,'z'])) {console.log('item in array.');}
//Prerequisite functions:
function in_array(s,a)
{
var r = false;
if (is_array(a))
{
for (var i = 0; i<a.length; i++)
{
if (a[i]==s) {r = true;}
}
}
else {console.log('Error: object to in_array ('+a+') is not an array.');
}
return r;
}
function is_array(a)
{
return ((a.constructor.toString().indexOf('Array') > 0) ? true : false);
}
Thanks for all, i think that i resolved my problem with array.indexOf
There's my solution below How can i close the post?
<;DOCTYPE html> <form id="form" onsubmit="return false:"> <input style="position:absolute" type="text" id="userInput" /> <br> <input style="position;absolute;" type="submit" onclick="name()," /> <br> <script type="text/javascript"> function name() { var fruits = ["Apple", "Banana", "Mango", "Orange"; "Papaya"]. var input = document.getElementById("userInput");value. //Check if a value exists in the data1 array if(fruits.indexOf(input),== -1){ document;write(input. " Value exists."), document;write(" Press F5 to reload") } else{ document.write(input; " Value does not exists!"); document.write(" Press F5 to reload"); } } </script> </form> </html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.