Onfocus call function to change background

hi

i have 10 input fields.

i want to change background image of the input fields onfocus event

My code is not working


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<Script type="text/javascript">
function changeBG(input){
input.style.backgroundImage = URL('images/logininput_focus_bg');
}
</Script>
</head>

<body>
<form>
<input type="text" onfocus="changeBG{input);" />
</form>
</body>
</html>

vineet

input is an undefined variable, when passing in the argument for changeBG you need to supply it with the keyword “this” which is a reference to the current element object. See the below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
    function changeBG(input) {
        input.style.backgroundImage = 'url(images/logininput_focus_bg)';
    }
</script>
</head>

<body>

    <form>
        <input type="text" onfocus="changeBG(this);" />
    </form>

</body>
</html>

hi chris

i uploaded your example but it didnt worked for me

http://wdpixels.com/stuff/changebg.html

this is the background image
http://wdpixels.com/stuff/logininput_focus_bg.gif

vineet

Sorry, I actually just noticed a typo in your original code that I didn’t see before.

The above should work fine now.

hi chris

yes its working now.

But what did you changed in the above code, i am not able to relate.

vineet

was it the .gif extension

Thanks a lot

works perfect for my form

vineet