PDA

View Full Version : Help to create universal clearbox, javascript function


hogtied
12-08-2002, 04:02 AM
Hello,

I'm trying to create a universal clearbox javascript function.

This function is intend to clear any text box that the user clicks on.

I have 3 textboxes that I would like cleared with one script,
when the user clicks inside the textbox. (Making the textbox have the focus)

<form name="newsletter" ACTION="" METHOD=POST>
<INPUT TYPE=HIDDEN NAME="src" VALUE="">
<p> <strong>Join our<br>
Hot Deals Newsletter</strong>
<input name="email" type="text" value="Enter your email here" onClick="clearbox('newsletter', 'email')">

<script language="JavaScript" type="text/JavaScript">
function clearbox(formID, boxID)
{
document.formID.boxID.value = ""
}
</script>

Thanks
Hotgtied

RadarBob
12-08-2002, 04:29 AM
Universal, eh? OK, how about this?

<form name="newsletter" ACTION="" METHOD=POST>
<INPUT TYPE=HIDDEN NAME="src" VALUE="">
<p> <strong>Join our<br>
Hot Deals Newsletter</strong>
<input name="email" type="text" value="Enter your email here" onClick="clearbox(this)">

<script language="JavaScript" type="text/JavaScript">
function clearbox(thebox)
{
thebox.value = "";
}
</script>

Your function "wastes it's time" by passing parameters. When you reference an input thing (textbox, checkbox,etc.) starting with "document..." , that's a "global reference" as I call it and parameters aren't needed. Because you pass the form, you don't need "document." in the function.

Personally I think passing parameters is as a general programming rule, a good thing.

I wonder if your version would run into problems if you had frames. I don't know I haven't used frames before.

hogtied
12-09-2002, 02:32 AM
Sweet. Thanks Radabob.. That works wonderfully..

Thanks
Hogtied