473,509 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Remove value of input element onfocus

Max
I've got a default value of some text in an input element when a page loads.
What I'd like to do is have the value disappear when the user clicks in the
input field.

I've figured out I can use:

onclick="this.value='';"

This works fine, however if for some reason the user adds his own input,
then click somewhere else/clicks back, the text has once again been erased.

So I thought of a function like the following, however it does not work:

function removeinput(x) {

if (x == 'Enter items not on standard list here...') {

x = '';

}
}

onclick="removeinput(this.value);"

I've also tried placing this.value inside of single quotes to no avail.

What am I doing wrong? Or do I just have the complete wrong approach here?

Thanks,
Max
Sep 22 '05 #1
5 19158
This should do what you are looking for:

<input name=tbTest type=text onfocus='tbTest_focus(event,this)'
value='enter some text' size=30>

<script>

function tbTest_focus(e,o){
if(o.firstTime){return}
o.firstTime=true
o.value=""
}

</script>

Sep 22 '05 #2
Lee
Max said:

I've got a default value of some text in an input element when a page loads.
What I'd like to do is have the value disappear when the user clicks in the
input field.

I've figured out I can use:

onclick="this.value='';"

This works fine, however if for some reason the user adds his own input,
then click somewhere else/clicks back, the text has once again been erased.

So I thought of a function like the following, however it does not work:

function removeinput(x) {

if (x == 'Enter items not on standard list here...') {

x = '';

}
}

onclick="removeinput(this.value);"

I've also tried placing this.value inside of single quotes to no avail.

What am I doing wrong? Or do I just have the complete wrong approach here?


The value attribute is a primitive string, so it is always passed
by value. In other words, "x" is a new variable containing the
same value as the argument. Changing it only changes the copy.

On the other hand, the keyword "this" is a reference to an Object, so
if you pass it, you can change the attributes of the Object that it
refers to. Also, you probably want to use the onfocus handler, not
the onclick:

function removeinput(x) {
if (x.value == 'Enter items not on standard list here...') {
x.value = '';
}
}

onfocus="removeinput(this);"

Sep 22 '05 #3
Max
> The value attribute is a primitive string, so it is always passed
by value. In other words, "x" is a new variable containing the
same value as the argument. Changing it only changes the copy.

On the other hand, the keyword "this" is a reference to an Object, so
if you pass it, you can change the attributes of the Object that it
refers to. Also, you probably want to use the onfocus handler, not
the onclick:

function removeinput(x) {
if (x.value == 'Enter items not on standard list here...') {
x.value = '';
}
}

onfocus="removeinput(this);"


Thanks a bunch. It works great, and now I understand a bit more about the
concept.
Sep 23 '05 #4
Max
> <input name=tbTest type=text onfocus='tbTest_focus(event,this)'
value='enter some text' size=30>

<script>

function tbTest_focus(e,o){
if(o.firstTime){return}
o.firstTime=true
o.value=""
}

</script>


This worked great as well as the second responder. I guess this one is more
portable and not dependant on what the default value of the input element
is.
Sep 23 '05 #5
Max wrote:
<input name=tbTest type=text onfocus='tbTest_focus(event,this)'
value='enter some text' size=30>

<script>

function tbTest_focus(e,o){
if(o.firstTime){return}
o.firstTime=true
o.value=""
}

</script>

This worked great as well as the second responder. I guess this one is more
portable and not dependant on what the default value of the input element
is.


Except that if your form has a reset button, it will set the value back
to the default and subsequent focuses won't clear the value. There
seems no point in passing 'event' as it isn't used for anything.

Try this version another:

<form action="">
<input name="tbTest" type="text"
onfocus="tbTest_focus(this);"
value="enter some text" size="30">
<input type="reset">
</form>

<script type="text/javascript">

function tbTest_focus(el)
{
if(el.V) {
if (el.value == el.V) {
el.value = '';
}
} else {
el.V = el.value;
el.value = '';
}
}
</script>
--
Rob
Sep 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
11463
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not...
1
3366
by: MickG | last post by:
I am trying to change the value of the variable "hard" according to which radio button is pressed and I am having no joy. Could anyone help me with this, the problematic section is marked with...
2
2167
by: Alexandre Jaquet | last post by:
Hi, Under IE I could not point why I can't get the selected value from a function here the code function Lvl_P2P(url,closeIt,delay){ //ver1.0 4LevelWebs var fabricant_box; var...
5
2934
by: WilliamRLinden | last post by:
Hi world! we are pretty new to JavaScript and have been struggling for now 2 days on this problem ... We would appreciate mercy if anyone can give us some. Basically we are trying to simulate...
1
15314
by: Armin Gajda | last post by:
Hi, I have the following problem: An input field get a border assigned by a style class (e.g. 2px solid red). When the field gets the focus, we set the border to green. element.style.border...
14
5155
RMWChaos
by: RMWChaos | last post by:
Firebug is reporting "too much recursion" when I attempt to create a child element in a parent that doesn't exist yet. The script should automatically create the missing parent before going on to...
1
1299
by: kirtangl | last post by:
Hi I am kinda a noob when it comes to coding, but I have been doing some html, javascript etc. I'm creating a stopwatch (using a code I found and edited a little bit) But I need to make one...
10
2430
meenakshia
by: meenakshia | last post by:
hi forum:) i have a form in which i have four input fields for pieces to be entered and 4 fields for amount what i want is that the first pieces-t1 should be visible and rest three should not show...
2
2595
by: sumanta123 | last post by:
Dear Sir, In my develpment i am getting stuck for a senario.Kindly please help me for it. Everytime we give the request we get the response of 8 records and its corresponding value. Then next...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.