Reputation: 65
I have the following code.
data.forEach((element) => {
//nuova feat di ES6 , passando i dati in questo modo le mie const assumono il valore di element.confirmed , etc
console.log(element);
var {
confirmed,
countryregion,
location,
provincestate,
deaths,
recovered,
} = element;
// console.log(countryregion);
window.onkeyup = keyup;
//creates a global Javascript variable
var inputTextValue;
function keyup(e) {
//setting your input text to the global Javascript Variable for every key press
inputTextValue = e.target.value;
//listens for you to press the ENTER key, at which point your web address will change to the one you have input in the search box
if (e.keyCode == 13) {
if (inputTextValue == countryregion || inputTextValue == provincestate) {
return console.log("THIS OBJECT");
} else {
return console.log("THIS OBJECT DON'T EXIST");
}
}
}
I would like to make a search input. In practice I would like to make sure that when the element that the user inserts into the search input is equal to provincestate or countryregion it returns all the data relating to that object. For example if I wrote Morocco in the input I would return this object here with deaths , recovered and confirmed values.
Such as
Array data is a array of object from api. But I'm having various problems
in fact, the comparison is made only with the last element of provincestate or countryregion.. Can you help me ?
Upvotes: 1
Views: 307
Reputation: 136
if you don't want to use input tag
let countryregion = "abc";
let provincestate = "def";
let inputTextValue = "";
window.onkeyup = keyup;
document.getElementById("clear").onclick = function(){
inputTextValue = "";
document.getElementById("search").innerText = inputTextValue;;
}
function keyup(e) {
//setting your input text to the global Javascript Variable for every key press
//listens for you to press the ENTER key, at which point your web address will change to the one you have input in the search box
if (e.keyCode == 13) {
if (inputTextValue.toLowerCase() == countryregion.toLowerCase() || inputTextValue.toLowerCase() == provincestate.toLowerCase()) {
return console.log("THIS OBJECT " + inputTextValue);
} else {
return console.log("THIS OBJECT DON'T EXIST " + inputTextValue);
}
}else{
inputTextValue += String.fromCharCode(e.keyCode);
document.getElementById("search").innerText = inputTextValue;;
}
}
<div id="search">type text then press Enter</div>
<button id="clear">clear</button>
Upvotes: 1
Reputation: 136
let countryregion = "abc";
let provincestate = "def";
window.document.getElementById("inputHere").onkeyup = keyup;
//creates a global Javascript variable
var inputTextValue;
function keyup(e) {
//setting your input text to the global Javascript Variable for every key press
inputTextValue = e.target.value;
//listens for you to press the ENTER key, at which point your web address will change to the one you have input in the search box
if (e.keyCode == 13) {
if (inputTextValue == countryregion || inputTextValue == provincestate) {
return console.log("THIS OBJECT " + inputTextValue);
} else {
return console.log("THIS OBJECT DON'T EXIST");
}
}else{
}
}
<input id="inputHere"/>
Upvotes: 0
Reputation: 2474
Change your if condition as below, hope this will help
if (e.keyCode == 13) {
if (element['countryregion'] == inputTextValue || element['provincestate'] == inputTextValue) {
return console.log("THIS OBJECT");
} else {
return console.log("THIS OBJECT DON'T EXIST");
}
}
Upvotes: 0