Solved

clear text field when received focus

Posted on 2006-11-13
3
250 Views
Last Modified: 2008-02-01
hi, i need a script for a text field, when the text field received focus, anything in the text field will be remove and the user can enter his/her data without the need of deleting existing data?

can this be done? tks
0
Comment
Question by:InNoCenT_Ch1ld
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
rama_krishna580 earned 250 total points
ID: 17935506
Hi,

try this...

This function will clear all text fields on a page:

function clearAll(){
var texts=document.getElementsByTagName('input')
for (var i_tem = 0; i_tem < texts.length; i_tem++)
if (texts[i_tem].type=='text')
texts[i_tem].value=''
}

You can call it from a text input field like so:

<input type="text" onfocus="clearAll();">

If you want to clear all but the element being given focus, this function:

function clearAll(el){
var texts=document.getElementsByTagName('input')
for (var i_tem = 0; i_tem < texts.length; i_tem++)
if (texts[i_tem].type=='text'&&texts[i_tem]!==el)
texts[i_tem].value=''
}

and this call will work:

<input type="text" onfocus="clearAll(this);">

You could also simply reset the form(s) on your page but that would do more than simply clear the values of the text inputs. It depends upon what you want to do and why.

R.K
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 17935515
Or try this...

<html>
<head>
<title>Clear Form Demo</title>

<script language="javascript">
<!--
function ClearForm() {
  document.testform.email.value= "";
}

//-->
</script>

</head>

<body>

<form name="testform">
    <input type="text" name="email" value="Something here." onfocus="ClearForm();">
</form>
</body>
</html>

R.K
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 17935529
tks ;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now