simple javascript function

M

mgm

I want to write a function to clear any text box on any page.

How could I change this specific function (that has a form name) into
something that can take the form name in a parameter?

function erase(txtInput){
frmName.all(txtInput).value = '';
}

Thanks!
mgm
 
C

clintonG

Pass the parameter to document.getElementById( "elementID")
or document.getElementsByName("elementName").

Go to the JavaScript FAQ [1] where amongst many useful
code examples you can learn to use various methodologies to
clear textbox elements.

--
<%= Clinton Gallagher, "Twice the Results -- Half the Cost"
Architectural & e-Business Consulting -- Software Development
NET (e-mail address removed)
URL http://www.metromilwaukee.com/clintongallagher/

[1] http://irt.org/
 
M

Murray

Here's a slick way -

<input type="text" name="n" value="Your message"
onFocus="if(this.value=='Your message')this.value='';">

This field will ONLY clear if it contains "Your message" and you click in it
(give it focus), otherwise it stays filled in.
 
J

Jon Spivey

You can make that easier to implement by attaching the event handler with
script, eg say you had a form like this
<input type="text" name="a" value="box a">
<input type="text" name="b" value="box b">
<input type="text" name="c" value="box c">
then a script like this will clear the text box and restore the default
message onblur if nothing's typed in
<script>
function setForm(){
var a = document.forms[0].elements;
for(i=0;i<a.length;i++){if(a.type=='text'){
a.onfocus = function(){this.value='';}
a.onblur = function(){if(!this.value)this.value=this.defaultValue;}}}}
window.onload=setForm;
</script>
 
M

Murray

So - if nothing is typed in, why do you need to clear and reload it? 8)

--
Murray

Jon Spivey said:
You can make that easier to implement by attaching the event handler with
script, eg say you had a form like this
<input type="text" name="a" value="box a">
<input type="text" name="b" value="box b">
<input type="text" name="c" value="box c">
then a script like this will clear the text box and restore the default
message onblur if nothing's typed in
<script>
function setForm(){
var a = document.forms[0].elements;
for(i=0;i<a.length;i++){if(a.type=='text'){
a.onfocus = function(){this.value='';}
a.onblur = function(){if(!this.value)this.value=this.defaultValue;}}}}
window.onload=setForm;
</script>
 
J

Jon Spivey

you want to clear it when the user clicks in the field - so he can type
whatever he wants without having to delete the default value. Try the script
and it'll make sense :)

--
Cheers,
Jon
Microsoft MVP

Murray said:
So - if nothing is typed in, why do you need to clear and reload it? 8)

--
Murray

Jon Spivey said:
You can make that easier to implement by attaching the event handler with
script, eg say you had a form like this
<input type="text" name="a" value="box a">
<input type="text" name="b" value="box b">
<input type="text" name="c" value="box c">
then a script like this will clear the text box and restore the default
message onblur if nothing's typed in
<script>
function setForm(){
var a = document.forms[0].elements;
for(i=0;i<a.length;i++){if(a.type=='text'){
a.onfocus = function(){this.value='';}
a.onblur = function(){if(!this.value)this.value=this.defaultValue;}}}}
window.onload=setForm;
</script>

--
Cheers,
Jon
Microsoft MVP

Murray said:
Here's a slick way -

<input type="text" name="n" value="Your message"
onFocus="if(this.value=='Your message')this.value='';">

This field will ONLY clear if it contains "Your message" and you click
in it (give it focus), otherwise it stays filled in.

--
Murray

I want to write a function to clear any text box on any page.

How could I change this specific function (that has a form name) into
something that can take the form name in a parameter?

function erase(txtInput){
frmName.all(txtInput).value = '';
}

Thanks!
mgm

 
M

Murray

Jon:

Yep - I see. That's a keeper.

Mine does the same (with a different approach - I just don't reload the
default, which you could do with a similar onBlur that tests for a null
value). Also, it would need to be explicitly restated in each input tag.

--
Murray

Jon Spivey said:
you want to clear it when the user clicks in the field - so he can type
whatever he wants without having to delete the default value. Try the
script and it'll make sense :)

--
Cheers,
Jon
Microsoft MVP

Murray said:
So - if nothing is typed in, why do you need to clear and reload it? 8)

--
Murray

Jon Spivey said:
You can make that easier to implement by attaching the event handler
with script, eg say you had a form like this
<input type="text" name="a" value="box a">
<input type="text" name="b" value="box b">
<input type="text" name="c" value="box c">
then a script like this will clear the text box and restore the default
message onblur if nothing's typed in
<script>
function setForm(){
var a = document.forms[0].elements;
for(i=0;i<a.length;i++){if(a.type=='text'){
a.onfocus = function(){this.value='';}
a.onblur =
function(){if(!this.value)this.value=this.defaultValue;}}}}
window.onload=setForm;
</script>

--
Cheers,
Jon
Microsoft MVP

Here's a slick way -

<input type="text" name="n" value="Your message"
onFocus="if(this.value=='Your message')this.value='';">

This field will ONLY clear if it contains "Your message" and you click
in it (give it focus), otherwise it stays filled in.

--
Murray

I want to write a function to clear any text box on any page.

How could I change this specific function (that has a form name) into
something that can take the form name in a parameter?

function erase(txtInput){
frmName.all(txtInput).value = '';
}

Thanks!
mgm


 

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

Top