Solved

clear text field when received focus

Posted on 2006-11-13
3
252 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTML5 Looping table certain Cell 11 55
Mouse event to control image and transparency. 4 34
Adding Row and Cell on a table 14 25
Add or delete table rows 10 54
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

837 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