473,669 Members | 2,386 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.v alue='';"

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="remove input(this.valu e);"

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 19171
This should do what you are looking for:

<input name=tbTest type=text onfocus='tbTest _focus(event,th is)'
value='enter some text' size=30>

<script>

function tbTest_focus(e, o){
if(o.firstTime) {return}
o.firstTime=tru e
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="remov einput(this.val ue);"

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="remove input(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="remove input(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,th is)'
value='enter some text' size=30>

<script>

function tbTest_focus(e, o){
if(o.firstTime) {return}
o.firstTime=tru e
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,th is)'
value='ente r some text' size=30>

<script>

function tbTest_focus(e, o){
if(o.firstTime) {return}
o.firstTime=tru e
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
11481
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 an object... the function is like so: function calc_total() { var x,i,base,margin,total,newmargin,newtotal; base = document.frmKitAmount.txtTotalKitValue.value; margin = document.frmKitAmount.margin.value/100;
1
3408
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 ***********************, I've included all the code incase that isn't where the problem is. Any help would be hugely appreciated. Mick
2
2182
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 fabricant_value;
5
2950
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 the tab key when the down arrow key is pressed. (we know there are other way to control focus flow but we use a lot of dynamic jsp fields, that will make the flow control a nightmare, we just want basic tabbing from the arrow key)
1
15323
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 = "2px solid green"; When the field looses the focus, the border should change back to red.
14
5179
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 create the child element. At the point where the script is supposed to call itself with the new properties to create the parent, it gives me the error and looks like it's looping the function continuously. It seems that "id : attrib" (line 192) is...
1
1309
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 change, I want the text box's to change form one color to another when the time goes past 900seconds, or 15 minutes. Here is the script I'm using... <html> <head> </head>
10
2445
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 up on the form unless asked for. can anyone suggest me a way to do this i have come across a lot of help areas where we can add input fields but in my case i have only predefined 3 fields the code is below <td>Pieces-t1</td> <td><input...
2
2600
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 button will display more and more records if it is availbale in data base. I am attachcing the screen shoot of my problem CMP is a column in the this record.
0
8383
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8809
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8588
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.