I am trying to add an alert that says "not found" when you search for nothing using a search bar that I have already programmed in JavaScript. Something like this:
if (document.getElementById('query').value = *variable for nothing*) {
alert('Not found!')
}
This is example code. Can someone give me a line of code that would do this but is functional?
This is one of the few cases where the solution is to write no additional code at all!
You can examine the truthiness of the .value of your element to see if it has a value in it. That means you would check the condition:
if(document.getElementById('query').value) {
If that's truthy, then it has a value. If it's falsey then it does not have a value.
document.getElementById('test').addEventListener('click', () => {
if (document.getElementById('query').value) {
alert('It has a value');
} else {
alert('It does not have a value');
}
});
<input id="query" type="text" />
<button id="test">Test</button>