Clearing a textbox in a function call

T

threepins

Howdy,

Just wondering how the following might be done:

I have a couple of different webpages that need to call a function to
clear the textbox onFocus of the textbox. I'm wanting to use one
function that I can call from a javascript include file (rather than
having multiple versions of the function all through the site).

So far i've tried the following:

function clearfield(poForm,poElement)
{
if (document.forms[poForm].poElement.value == "Type Search Here...")
document.forms[poForm].poElement.value = "";
}

where the passing page calls the function as:

clearfield('formname','formelement');

but just get errors.
Any help on this one would be most appreciative!!

Cheers
Bj McGowan
 
D

Dietmar Meier

I have a couple of different webpages that need to call a function to
clear the textbox onFocus of the textbox.

whatever.js:

function clrTxt(oElem, oEvent) {
if (
oElem
&& oElem.value
&& oElem.defaultValue
&& oEvent
&& oEvent.type
) {
if (
oEvent.type == "focus"
&& oElem.value == oElem.defaultValue
) {
oElem.value = "";
}
else if (
oEvent.type == "blur"
&& oElem.value == ""
) {
oElem.value = oElem.defaultValue;
}
}
}

whatever.html:

....
<script type="text/javascript" src="whatever.js"></script>
....
<input ...
onfocus="clrTxt(this, event)"
onblur="clrTxt(this, event)"
value="Type Search Here..."....

ciao, dhgm
 
B

Bj

Thanks Dietmar,

Worked a treat!

Bj

Dietmar Meier said:
whatever.js:

function clrTxt(oElem, oEvent) {
if (
oElem
&& oElem.value
&& oElem.defaultValue
&& oEvent
&& oEvent.type
) {
if (
oEvent.type == "focus"
&& oElem.value == oElem.defaultValue
) {
oElem.value = "";
}
else if (
oEvent.type == "blur"
&& oElem.value == ""
) {
oElem.value = oElem.defaultValue;
}
}
}

whatever.html:

...
<script type="text/javascript" src="whatever.js"></script>
...
<input ...
onfocus="clrTxt(this, event)"
onblur="clrTxt(this, event)"
value="Type Search Here..."
...

ciao, dhgm
 
D

Dietmar Meier

Bj said:
Worked a treat!

Not really. Didn't test it before posting, and as I see now,
there's a mistake:

should, of course, be replaced with:

function clrTxt(oElem, oEvent) {
if (
oElem
&& typeof oElem.value != "undefined"
&& oElem.defaultValue
&& oEvent
&& oEvent.type
) {

ciao, dhgm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top