Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
94
rated 0 times [  101] [ 7]  / answers: 1 / hits: 30311  / 8 Years ago, thu, june 23, 2016, 12:00:00

I'm following a Javascript tutorial using a book.
The exercise is change the background color in a <input type=search> using document.querySelector. When I try to search something with no text in the search box, the background from <input> changes to red. I did it using onsubmit and some conditional. But in the next part, it must returns to white bckground using onfocus and I'm not getting.



The code that I tried is



document.querySelector('#form-busca').onsubmit = function() {
if (document.querySelector('#q').value == '') {
document.querySelector('#q').style.background = 'red';
return false;
}
}

document.querySelector('#form-busca').onfocus = function() {
document.querySelector('#q').style.background = 'white';
}


Can someone help me? Thanks a lot!


More From » onfocus

 Answers
112

almost got it dude.



change:



document.querySelector('#form-busca').onfocus


to:



document.querySelector('#q').onfocus



revised code:



correct sample:



document.querySelector('#form-busca').onsubmit = function() {
if (document.querySelector('#q').value == '') {
document.querySelector('#q').style.background = 'red';
return false;
}
}

document.querySelector('#q').onfocus = function() {
document.querySelector('#q').style.background = 'white';
}

[#61662] Tuesday, June 21, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
blaisep

Total Points: 748
Total Questions: 95
Total Answers: 108

Location: Federated States of Micronesia
Member since Sun, May 16, 2021
3 Years ago
blaisep questions
Wed, Dec 16, 20, 00:00, 4 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
Tue, Nov 12, 19, 00:00, 5 Years ago
Mon, Nov 11, 19, 00:00, 5 Years ago
;