0

I have an html file and I want to grab the text that is entered in the input field using javascript. I have tried a few different ways but nothing seems to be working properly. Here is the code that I have right now.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <title>Document</title>
</head>
<body>
    <input type="text" id="searchingterm">
    <form>
        <input type="text" id="search">
    </form>
    <script>
        $(document).ready(function(){
            var searched = document.getElementById("search").value;
            console.log(searched)
        })
    </script>
</body>
</html>

i also tried using jquery but that didnt do anything either.

1
  • The input doesn't have a value, and you haven't attached this to any sort of handler (click, change). What are your intentions? Commented Jan 17, 2019 at 0:08

1 Answer 1

2

basically, the problem is that you are not assigning anything to that input and also you dont have a function to trigger the search.

your code was right, you just needed to change it a little bit.

function search() {
  var searched = document.getElementById("search").value;
  console.log(searched)
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <title>Document</title>
</head>

<body>
  <form>
    <input type="text" id="search">
  </form>
  <button onclick="search()"> search</button>
</body>

</html>

here having the script on the body

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <title>Document</title>
</head>

<body>
  <form>
    <input type="text" id="search">
  </form>
  <button onclick="search()"> search</button>
  <script>
    function search() {
      var searched = document.getElementById("search").value;
      console.log(searched)
    }
  </script>
</body>

</html>

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

2 Comments

where should I put the function. If i put it in a script tag above the html and when i run it i get this: (index):25 Uncaught TypeError: search is not a function at HTMLButtonElement.onclick ((index):25)
@OmarJandali I added another snippet with the function inside the script tag

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.