mag1c1an
asked on
onFocus delete text from textbox
Hi,
I have a form like this;
<form name="form1" method="post" action="process_applicatio ns.php">
<!-- Lots of form fields here like radio buttons etc-->
<input type="text" name="custom[1]" value="Special reason" size=15>
<input type="text" name="custom[12]" value="Special reason" size=15>
<input type="text" name="custom[4]" value="Special reason" size=15>
<input type="text" name="custom[85]" value="Special reason" size=15>
<input type="text" name="custom[11]" value="Special reason" size=15>
</form>
What I want to do is, on focus if custom[x] that textboxs value should be deleted if the value is not "Special reason"
I used to use something like this before:
<script>
function clearText(thefield){
if (thefield.defaultValue==th efield.val ue)
thefield.value = ""
}
</script>
but its not working for some reason with the above form even after I tried modifying it....
Thanks,
Mag
I have a form like this;
<form name="form1" method="post" action="process_applicatio
<!-- Lots of form fields here like radio buttons etc-->
<input type="text" name="custom[1]" value="Special reason" size=15>
<input type="text" name="custom[12]" value="Special reason" size=15>
<input type="text" name="custom[4]" value="Special reason" size=15>
<input type="text" name="custom[85]" value="Special reason" size=15>
<input type="text" name="custom[11]" value="Special reason" size=15>
</form>
What I want to do is, on focus if custom[x] that textboxs value should be deleted if the value is not "Special reason"
I used to use something like this before:
<script>
function clearText(thefield){
if (thefield.defaultValue==th
thefield.value = ""
}
</script>
but its not working for some reason with the above form even after I tried modifying it....
Thanks,
Mag
<input type="text" name="custom[11]" value="Special reason" size=15 onfocus='if(this.value!="S pecial reason")this.value="";' />
reversing the quotes for convenience:
<input type="text" name="custom[11]" value="Special reason" size=15 onfocus="if(this.value!='S pecial reason')this.value='';" />
<input type="text" name="custom[11]" value="Special reason" size=15 onfocus="if(this.value!='S
ASKER
Damn...i must have posted this 10 seconds before you responded!!!
Problem is....its not working :-(
I tried it in Firefox and IE6...
Thanks,
Mag
Problem is....its not working :-(
I tried it in Firefox and IE6...
Thanks,
Mag
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok, got it...I just changed the != to == and it works...
oh ... ok good! I must have mis-understood the question.
ASKER
Maybe...but you helped me find the solution....full points and my thanks!
:-)
Mag
:-)
Mag
ASKER
I was kind of hopeing for onBlur="functionName()" as its less code and the page would load faster.... (coz upto 50 of these textboxes can be on the page...) if you get time please see if thats possible...either way I have already awarded you the points.
Thanks,
Mag
Thanks,
Mag
no problem ... stand by
function myFunction(field)
{
if( field.value == field.defaultValue )
field.value="" ;
}
<input type="text" name="custom[11]" value="Special reason" size="15" onfocus="myFunction(this); " />
{
if( field.value == field.defaultValue )
field.value="" ;
}
<input type="text" name="custom[11]" value="Special reason" size="15" onfocus="myFunction(this);
Reduce the size by script event handler assignment.
Like this:
<head>
<script>
function clearText(thefield){
if (thefield.defaultValue!=th efield.val ue){
thefield.value = ""
}
}
function init(){
var elem = document.form1.elements;
for(var i=0;i<elem.length;i++){
if(elem[i].name.indexOf("c ustom[")== 0){
elem[i].onfocus=function() {clearText (this)}
}
}
}
</script>
</head>
<body onLoad="init()">
<form name="form1" method="post" action="process_applicatio ns.php">
<!-- Lots of form fields here like radio buttons etc-->
<input type="text" name="custom[1]" value="Special reason" size=15>
<input type="text" name="custom[12]" value="Special reason" size=15>
<input type="text" name="custom[4]" value="Special reason" size=15>
<input type="text" name="custom[85]" value="Special reason" size=15>
<input type="text" name="custom[11]" value="Special reason" size=15>
</form>
</body>
Like this:
<head>
<script>
function clearText(thefield){
if (thefield.defaultValue!=th
thefield.value = ""
}
}
function init(){
var elem = document.form1.elements;
for(var i=0;i<elem.length;i++){
if(elem[i].name.indexOf("c
elem[i].onfocus=function()
}
}
}
</script>
</head>
<body onLoad="init()">
<form name="form1" method="post" action="process_applicatio
<!-- Lots of form fields here like radio buttons etc-->
<input type="text" name="custom[1]" value="Special reason" size=15>
<input type="text" name="custom[12]" value="Special reason" size=15>
<input type="text" name="custom[4]" value="Special reason" size=15>
<input type="text" name="custom[85]" value="Special reason" size=15>
<input type="text" name="custom[11]" value="Special reason" size=15>
</form>
</body>
ASKER
Hi guys,
Thanks! This is working fine for me:
<script>
function myFunction(field)
{
if( field.value == field.defaultValue )
field.value="" ;
}
</script>
I dont really understand the second part (the one with init() ) and i dont think i can use it coz I dont have control over the <body> tag....this whole thing goes into a php script and the client will be able to pick the header template and the footer template....the <body> tag i guess will be in the header template...
See you guys later.
Mag
Thanks! This is working fine for me:
<script>
function myFunction(field)
{
if( field.value == field.defaultValue )
field.value="" ;
}
</script>
I dont really understand the second part (the one with init() ) and i dont think i can use it coz I dont have control over the <body> tag....this whole thing goes into a php script and the client will be able to pick the header template and the footer template....the <body> tag i guess will be in the header template...
See you guys later.
Mag