Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
28
rated 0 times [  32] [ 4]  / answers: 1 / hits: 193297  / 11 Years ago, thu, june 20, 2013, 12:00:00

I am having a tough time with this javascript code to change the background color of a text input if the input is empty.



Here is the code:



function checkFilled() {
var inputVal = document.getElementById(subEmail).value;
if (inputVal == ) {
inputVal.style.backgroundColor = yellow;
}
}


Example: http://jsfiddle.net/2Xgfr/



I would expect the textbox to come out yellow at the beginning.


More From » text

 Answers
85

DEMO --> http://jsfiddle.net/2Xgfr/829/



HTML



<input type=text id=subEmail onchange=checkFilled();>


JavaScript



 function checkFilled() {
var inputVal = document.getElementById(subEmail);
if (inputVal.value == ) {
inputVal.style.backgroundColor = yellow;
}
else{
inputVal.style.backgroundColor = ;
}
}

checkFilled();


Note: You were checking value and setting color to value which is not allowed, that's why it was giving you errors. try like the above.


[#77514] Wednesday, June 19, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
patienceannel

Total Points: 674
Total Questions: 101
Total Answers: 101

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
;