clearing a textbox and filling with a function

R

RelaxoRy

If I fill out the textfields properly with Name, Email etc in a row it
is fine. But if I fill out, for example, my Name, then tab twice, it
clears my entered value. How do I stop this?

All I want the script to do is that when the cell is blank or
whitespace, put the necessary text in the field, else, continue as
normal.


--- HTML Extract
<td width="74%">
<input name="name" type="text" class="textbox" id="name"
onMouseOver="this.style.backgroundColor = 'CDEDDD';"
onMouseOut="this.style.backgroundColor = 'E2E2E2';"
onfocus="this.style.backgroundColor = 'CDEDDD'; this.style.color =
'000000';textual(name,this.value);"
onblur="this.style.backgroundColor = 'E2E2E2'; this.style.color =
'000000';textual(name,this.value);"
value="Enter Name" size="30"></td></tr>
-- END Extract


function textual(element,text) {

var name,company,email,worknumber,homenumber,query

// If the Field contains the default text, clear it ready for new text
if (text=="Enter Name") { document.form1.name.value = "" }
if (text=="Enter Company Name") { document.form1.company.value = "" }
if (text=="Enter Email Address") { document.form1.email.value = "" }
if (text=="Enter Work Number") { document.form1.worknumber.value = ""
}
if (text=="Enter Home Number") { document.form1.homenumber.value = ""
}
if (text=="Enter your query here...") { document.form1.query.value =
"" }

// If the Field is blank, re-write the contents
if (text=="") {
if (element="name") { document.form1.name.value = "Enter Name" }
if (element="company") { document.form1.company.value = "Enter
Company Name" }
if (element="email") { document.form1.email.value = "Enter Email
Address" }
if (element="worknumber") { document.form1.worknumber.value = "Enter
Work Number" }
if (element="homenumber") { document.form1.homenumber.value = "Enter
Home Number" }
if (element="query") { document.form1.query.value = "Enter your query
here..." }
}

}
 
M

Michael Winter

If I fill out the textfields properly with Name, Email etc in a row it
is fine. But if I fill out, for example, my Name, then tab twice, it
clears my entered value. How do I stop this?

All I want the script to do is that when the cell is blank or
whitespace, put the necessary text in the field, else, continue as
normal.

[snipped code]

You can modify your code to be considerably smaller:

function clearInput( control ) {
if( control.defaultValue == control.value ) {
control.value = '';
}
}

function restoreInput( control ) {
if( '' == control.value ) {
control.value = control.defaultValue;
}
}

<input ... onfocus="clearInput(this)" onblur="restoreInput(this)">

Hope that helps,
Mike
 
R

relaxory

Ahhh of course!
It worked a treat. Must keep remembering everything is recorded.

Thanks.
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top