Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Why am i getting undefined in the console log instead of the input value?

I am trying to get the input value as my result but after clicking on the button I am getting 'undefined' as the console result. Please help to understand the reason. Sharing the HTML and js code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="movie-search-engine.css">
    <script src="scripts/movie-search-engine.js" defer></script>
    <title>Movie Search Engine</title>
</head>
<body>
    <div class="searchBox">
        <input type="text" class="searchBox" placeholder="Movie Name">
        <button onclick="moviesearchEngine()" class="searchBoxID" >Search</button>
    </div>
    <div class="movieDetails">
        <h2 class="movieTitle">Movie Title</h2>
        
        <div class="movieData">
            <p class="yearofRelease">Year of Release</p>
            <p class="movieTitle">Movie Title</p>
            <p class="genre">Genre</p>
            <p class="director">Director</p>
            <p class="plot">Plot</p>
        </div>
</body>
</html>


const moviesearchEngine=()=>{
    let searchBox = document.querySelector('.searchBox');
    console.log(searchBox.value)
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are using a class for multiple elements which is why undefined is shown. Among many ways, I will discuss two here, you have to either use indices with classes if using queryselector to get the correct element or use an ID to select the input element. Here I have used ID to get the input searchbox. See the following code:

const moviesearchEngine = () => {
  let searchBox = document.querySelector('#searchBoxInput');
  console.log(searchBox.value)
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="movie-search-engine.css">
  <script src="scripts/movie-search-engine.js" defer></script>
  <title>Movie Search Engine</title>
</head>

<body>
  <div class="searchBox">
    <input type="text" id="searchBoxInput" placeholder="Movie Name">
    <button onclick="moviesearchEngine()" class="searchBoxID">Search</button>
  </div>
  <div class="movieDetails">
    <h2 class="movieTitle">Movie Title</h2>

    <div class="movieData">
      <p class="yearofRelease">Year of Release</p>
      <p class="movieTitle">Movie Title</p>
      <p class="genre">Genre</p>
      <p class="director">Director</p>
      <p class="plot">Plot</p>
    </div>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Report

0

Your div <div class="searchBox"> also contain a class "searchBox" so it selected that.

Fixed: Add an input selector after class selector.

const moviesearchEngine=()=>{
    let searchBox = document.querySelector('.searchBox input');
    console.log(searchBox.value)
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="movie-search-engine.css">
    <script src="scripts/movie-search-engine.js" defer></script>
    <title>Movie Search Engine</title>
</head>
<body>
    <div class="searchBox">
        <input type="text" class="searchBox" placeholder="Movie Name">
        <button onclick="moviesearchEngine()" class="searchBoxID" >Search</button>
    </div>
    <div class="movieDetails">
        <h2 class="movieTitle">Movie Title</h2>
        
        <div class="movieData">
            <p class="yearofRelease">Year of Release</p>
            <p class="movieTitle">Movie Title</p>
            <p class="genre">Genre</p>
            <p class="director">Director</p>
            <p class="plot">Plot</p>
        </div>
</body>
</html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!