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.
3
Answers
You are very close, you have the element, you just need to extract the value from it, using the
.value
property:You can read the input of it like this:
Also I recommend using
let
instead ofvar
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")