...

View Full Version : Where is the error in this script?



bostjank
08-12-2005, 09:40 AM
Hi!

I have problem with this script - it should toggle background image of a textbox on focus and blur events (checking whether textbox value is "").
I keep getting "Object does not support this property" error.

I would appreciate your help.
Bostjan

See code below and images in the attachment.

<SCRIPT>
function sh_box(hide) {
txtBox1 = document.test.getElementByID("txtBox");
if(hide == "true")
txtBox1.style.backgroundImage = "url(image_on.gif)";
else
txtBox1.style.backgroundImage = (txtBox1.value == "" ? "url(image_on.gif)" : "url(image_off.gif)");
}
</SCRIPT>
<form method="POST" action="" name="test">
<p>
<input type="text" name="txtBox" size="60" style="background-image: url('image_off.gif') " onFocus="sh_box('true');" onBlur="sh_box('false');"></p>
</form>

iota
08-12-2005, 10:15 AM
Does this suit your wish ? :)





<style>
#off{background-image: url('image_off.gif');}
#on {background-image:url('image_on.gif');}
</style>

<form method="POST" action="" name="test">
<input type="text" name="txtBox" id="off" size="60" onFocus="document.forms[0].txtBox.id='on'"
onBlur="document.forms[0].txtBox.id='off'">
</form>

bostjank
08-12-2005, 10:43 AM
thanks, iota.

Your code generally does what I need, except for I thing: on blur event background image should't be automatically changed back to start image if there is some text in the text box.

Bostjan

charlene
08-12-2005, 11:07 AM
<SCRIPT>
function sh_box(hide) {

var oTextBox = document.getElementById("txtBox");
if(hide == "true")
oTextBox.style.backgroundImage="url('image_on.gif')";
else
oTextBox.style.backgroundImage = (oTextBox.value == "" ? "url(image_on.gif)" : "url(image_off.gif)");
}
</SCRIPT>
<form method="POST" action="" name="test" ID="Form1">

<input type="text" id="txtBox" name="txtName" size="60" style="background-image: url('image_off.gif') "
onFocus="sh_box('true');" onBlur="sh_box('false');" >
</form>

iota
08-12-2005, 11:11 AM
Sorry, I've misunderstood your wish. :rolleyes:
Hope this will surely meets your need. :p






<style>
#off{background-image: url('image_off.gif');}
#on {background-image:url('image_on.gif');}
</style>

<script>

function checktxt() {

if (document.forms[0].txtBox.value == "")

{

document.forms[0].txtBox.id='off'
}

else

{
document.forms[0].txtBox.id='on'
}

}

</script>
<form method="POST" action="" name="test">
<input type="text" name="txtBox" id="off" size="60" onFocus="document.forms[0].txtBox.id='on'" onBlur="checktxt()">
</form>

bostjank
08-12-2005, 11:23 AM
Thanks to both. It now works fine.

Bostjan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum