So, I have a form that consists of 4 inputs, username, password, email, and name.
I am sick and tired of having to make a javascript function for each of them, because each input is a text input, and when a user clicks the input box I have it change the background of the input to a different color.
So heres how I coded:
The form inputs:
<input type="text" name="username" id="usernameInput" onclick="changeUsername(); return false;" onblur="changeUsernameback(); return false;">
<input type="text" name="password" id="passwordInput" onclick="changePassword(); return false;" onblur="changePasswordback(); return false;">
and the other two forms are the same, only with different names and different id's and javascript functions.
MY Javascript:
function changeUsername() {
document.getElementById('usernameInput').style.background='#FFFF00';
}
function changeUsernameBack() {
document.getElementById('passwordInput').style.background='#FFFF00';
}
and the other three are just like that only setup for their own specific id.
AND when creating CSS, I have to make different ID's for all 4 inputs.
What I want to know is: Is there a way I can only make one CSS id, and one javascript function to change all inputs? Because I know when you just use one function for all, javascript tries to change all at once..
I was thinking something like
document.getElementById('inputText'+[i]).style.background='#FFFF00';
and then when I give each input an id I could just automatically increment them on the page such as input1, input2, input3 etc.
But that doesn't seem to work? Maybe I am coding it wrong? Please help..