Solved

clear text field when received focus

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

685 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