2

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>Data search</title>
   </head>
   <body>
      <input type="text" name="" id="inputData" value="" placeholder="">
      <button type="button" onclick="myFunction()">search</button>
      <p id="text"></p>
      <script type="text/javascript">
         function myFunction(){
          var data = [1, 2, 3, 4, 5, "name", "email", "id", "password", "position"];
          var iData = document.getElementById("inputData").value;
         
          for(var i = 0; i <= data.length; i++){
             
              if(iData == data){
         
                  document.getElementById("text").innerHTML = "This data is available in array data";
              }else{
         
                   document.getElementById("text").innerHTML = "This data is not available in array data";
              }
          }
         }
      </script>
   </body>
</html>

I created an array which contains some data. I take input from a text field and search the input against the data in the array. If the inputted data matches an element in the array, I want to show a notification such as:

"This data is available in array data"

If the input doesn't match any elements in the array, instead I want it to output:

"this data is not available in array data".

But it is not working. I am failing to find out the problem, and I am also uncertain whether this is the right way to get my expected result.

1 Answer 1

1

There are two errors in your code - first, when you find the data in the array, you need to immediately return. Otherwise your function will keep looping through elements and overwrite the positive output again with "This data is not available in array data".

Secondly, in your comparison you need to use data[i] instead of data, because you want to test against each element in the array, rather than the entire array itself.

Here is a live, working example:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>Data search</title>
   </head>
   <body>
      <input type="text" name="" id="inputData" value="" placeholder="">
      <button type="button" onclick="myFunction()">search</button>
      <p id="text"></p>
      <script type="text/javascript">
         function myFunction(){
          var data = [1, 2, 3, 4, 5, "name", "email", "id", "password", "position"];
          var iData = document.getElementById("inputData").value;
         
          for(var i = 0; i <= data.length; i++){
             
              if(iData == data[i]){
         
                  document.getElementById("text").innerHTML = "This data is available in array data";
                  return;
              }else{
         
                   document.getElementById("text").innerHTML = "This data is not available in array data";
              }
          }
         }
      </script>
   </body>
</html>

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

Comments

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.