0

I'm trying to code a search input.

I took values from the

and input to JS code and assigned them to a variable.

I get an error: "cannot read property 'value' of null".

I don't know how to solve it. Tried many things.

I have tried changing the array variable to ["document.getElementById("text").value;"].

function search() {

  var textArray = document.getElementById("text").value;
  var newArray = [textArray];
  var inputValue = document.getElementById("searchInt").value;
  var search = newArray.indexOf(inputValue);

  if (search == -1) {
    alert("No search results!");
  } else {
    document.getElementById("lorem").innerHTML = search;
  }

}
<!DOCTYPE html>

<html>

<head>
  <title>Practice</title>
  <script src="practice.js"></script>
  <style src="practice.css"></style>
</head>

<body>
  <div id="container">

    <input type="text" id="searchInt" placeholder="Search..." />
    <button type="button" id="searchBtn" onclick="search();">Search</button>

    <div class="container2">
      <p id="lorem">What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make
        a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
        and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
      </p>
    </div>

  </div>
</body>

</html>

I need to load the DOM before the script, but I really don't know how to do it, other than write at the end of the body. I don't know other way with imported JS file.

I also don't want to use Jquery.

Slai
  • 19,980
  • 5
  • 38
  • 44
Robust
  • 65
  • 4

0 Answers0