I want a way to open a search through Duckduckgo
using the text
entered in a text input (# search-bar), so that when you click on the Enter
key the written text is the value of the search. I want to do it without using libraries like jQuery
.
This is my HTML code
<form>
<input type="text" id="search-bar" placeholder="Buscan">
<--
Si utilizo un <textarea></textarea> en lugar de un input, el javascript funciona,
pero no es lo que necesito.
-->
</form>
And this is my javascript.
var search_bar = document.getElementById("search-bar");
var search_wrap = document.getElementById("search-wrap");
var search_visible = false;
search_wrap.style.display = 'none';
"use strict";
document.onkeyup = function (e){
// if S
if((e.keyCode == 83) && (search_visible == false)){
search_visible = true;
search_wrap.style.display = 'block';
search_bar.focus();
}
// if Esc
else if((e.keyCode == 27) && (search_visible == true)){
search_visible = false;
search_wrap.style.display = 'none';
}.
// if Enter
// Si cambio el 13 (Enter) por otra tecla, E.g. 20 (Bloq Mayus) funciona a la perfección.
// Si cambio el 13 por un 17 (Ctrl) funciona haciendo Ctrl+Enter
else if((e.keyCode == 13) && (search_visible == true)){
var content = search_bar.value.replace(new RegExp(" ", 'g'), "+");
// He intentado con if(content.length > 0)
if(content != ""){
window.location = ("https://duckduckgo.com/?q=" + content + "&ia=about");
}
}
}
My problem is that the function does not run correctly if I use the Enter key.