skip to Main Content

Basically, there is an input tag that has the id bar, and it’s a search input.

<input id="bar" type="search">

and I tried to get the value of that input with jQuery.

var query = document.getElementById("bar");

And it opens a new tab that searches whatever you typed into the search bar into google. But it doesn’t get the text, it just says ‘undefined’. And no, I typed stuff.

Repl

3

Answers


  1. You are very close, you have the element, you just need to extract the value from it, using the .value property:

    <input id="bar" type="search">
    
    var query = document.getElementById("bar").value;
                                              ^^^^^^ 
    
    Login or Signup to reply.
  2. You can read the input of it like this:

    var query = document.getElementById('bar').value;
    

    Also I recommend using let instead of var

    Login or Signup to reply.
  3. Just add ".value" like this:
    let query = document.getElementById("bar").value;

    Also, try to use template literals for concatenating strings with variables for more readability.

    Here if you want to read more about Template literals.
    So that your concatenation would like:

    window.open(`https://www.google.com/search?q=${query}`,"Result")

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search