How To Search An Item In Javascript Array
In This Javascript Tutorial we will see How To Search And Find Item In An Array Using JS Using Netbeans Editor .
Project Source Code:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript: Search Element In Array</title>
<meta charset="windows-1252">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
var myArr = ["JavaScript","Search","Element","In","Array"];
function search()
{
var txt = document.getElementById('txt').value;
var index = myArr.indexOf(txt);
if(index !== -1)
{
alert('Exist');
}else{
alert('Not Exist');
}
}
</script>
</head>
<body>
<input type="text" name="txt" id="txt" />
<button onclick="search();">Search</button>
<p id="pgh"></p>
<script>
document.getElementById('pgh').innerHTML = myArr;
</script>
</body>
</html>