12

I want to clear the text field when the user clicks on that

<input name="name" type="text" id="input1" size="30" maxlength="1000" value="Enter Postcode or Area" onfocus=="this.value=''" />

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Gaurav Srivastava
  • 151
  • 1
  • 2
  • 11
  • 2
    Please don't do that. If I want to fix one mistyped letter, I do not want to have to retype the entire thing. (Most people who want to do this are misusing the value attribute to replace a ` – Quentin Mar 16 '11 at 10:47
  • 1
    Is this to clear a default value or sample text that initially appears on the form? – Marc Audet Mar 16 '11 at 10:49
  • @David Dorward: look at the search box at the top of this page. I'm sure Gaurav just wants to clear the initial value, not what the user types in... – Nicolas Le Thierry d'Ennequin Mar 16 '11 at 10:51
  • @Glauber — That might be what he wants, but it isn't what he asked for. There are four answers so far; all of them have the problem described in my comment. Abusing the value for the label might be fashionable, but it isn't good and it isn't accessible. – Quentin Mar 16 '11 at 10:54
  • @Glauber Rocha It only clear the `search` keyword. If you fill something in it, it will not be cleared when the field gain the focus again. – Romain Linsolas Mar 16 '11 at 10:55
  • For semantic reasons, it may be a good idea to use the value in a ` – Nicolas Le Thierry d'Ennequin Mar 16 '11 at 10:55
  • And put a transparent empty input-field on top of the label and making it opaque at first focus. Giving the illusion it is filled with a text hint. – Captain Giraffe Mar 16 '11 at 11:03

9 Answers9

22

Unless you are doing something specific where you only want to clear onclick, I would suggest (as others have noted) to use the onfocus actions instead. This way if someone is using tab to navigate it will also clear the default text.

You can also use onblur to check if it's empty to bring it back:

 <input type="text" value="Default text" name="yourName" onfocus="if(this.value == 'Default text') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Default text'; }">
ORyan
  • 482
  • 3
  • 8
9

To do this you will need to use a scripting language, probably javascript. Here an example

<input type='text' value'Some text' onclick='javascript: this.value = ""' />

Hope this helps.

Edit:

To meet what David is explain here is a second example in case that is what you are looking for

<script type='javascript'>
    var clear = true;
    function clear(obj)
    {
        if(clear)
        {
            obj.value = '';
            clear = false;
        }
    }
</script>

<input type='text' value'Some text' onfocus='clear(this);' />
Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
  • I would advise no one ever use this method. It is a terrible way to go about this. Please do not use this method. The problem with this example is that every time someone clicks on this input the contents will be cleared. Even if the user types something and then comes back to edit the field, the text will be cleared. Another problem is that if a user tabs to the input, the content is not being "clicked" on, and therefore does not clear itself. With both those issues this example is a terrible way to solve this problem and creates all sorts of accessibility problems. – ORyan Mar 13 '14 at 23:35
  • @ORyan, This initial code was the answer to the initial question. After more information was give the answer was edited and in that example the test is only cleared on the first click. So I hardly think its terrible. Yes it only did a click so I've updated it so you can sleep better tonight. – Ash Burlaczenko Mar 14 '14 at 09:49
  • @AshBurlaczenko, Even with this edit I would still advise against this method. The second example doesn't solve the issue and still has the possibility of clearing user data that may already be in the input. Your example will only work if there is one input on the page to be "cleared." As you are checking against a single bool that is set to true after the first input has been focused. A better method would be to check if the input contains the pre-populated content. `` – ORyan Mar 21 '14 at 22:31
5

Using jQuery library:

<input id="clearme" value="Click me quick!" />

$('#clearme').focus(function() { 
  $(this).val(''); 
});
Jhourlad Estrella
  • 3,545
  • 4
  • 37
  • 66
4

Or you can simply use the placeholder attribute For example<input name="name" type="text" id="input1" size="30" maxlength="1000" placeholder="Enter Postcode or Area"/>

  • 2
    A [simple CSS trick](https://css-tricks.com/snippets/css/style-placeholder-text/) can even style the placeholder in recent browser. In Chrome: `::-webkit-input-placeholder {color: #000;}` – Alrekr Sep 28 '16 at 08:51
3

You can use <input ... onfocus="this.value='';"/>.

This way, the field will be cleared when it gains focus. However, if you only want to clear it when user clicks on it (i.e. not when the field gains focus with the keyboard for example), then use onclick instead of onfocus.

However, as pointed by David Dorward in a comment, this behavior may not be expected by the user. So be careful to set this feature on really specific fields (such as search field).

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
1

This is how I use it for a temperature converter/calculator - when the user types (keyup), the text input box calculates using the assigned function; when the user selects the other text input (there are only two inputs), the selected text input will clear.

HTML:

<p class="celcius"><h2 style="color:#FFF">Input:</h2>
    <input name="celsius" type="text" class="feedback-input" placeholder="Temperature (Celsius)" onkeyup="Conversion()" onfocus="this.value='';" id="celsius" />
</p>

  <hr>

  <h2 style="color:#FFF">Result:</h2>

<p class="fahrenheit">
    <input name="fahrenheit" type="text" class="feedback-input" id="fahrenheit" onkeyup="Conversion2()" onfocus="this.value='';"placeholder="Temperature (Fahrenheit)" />
  </p>  

JavaScript:

function Conversion() {
    var tempCels = parseFloat(document.getElementById('celsius').value);
      tempFarh =(tempCels)*(1.8)+(32);
      document.getElementById('fahrenheit').value= tempFarh;
 }

function Conversion2() {
    var tempFarh = parseFloat(document.getElementById('fahrenheit').value);
      tempCels =(tempFarh - 32)/(1.8);
      document.getElementById('celsius').value= tempCels;
 }
rockmandew
  • 820
  • 13
  • 22
1

try this ,it worked for me

add this into your input tag

<code>
onfocus="this.value='';"</code>

for example if your code is

<code>
<input type="text" value="Name" /></code>

use it like this

<code><input onfocus="this.value='';" type="text" value="Name" /></code>
falsetru
  • 357,413
  • 63
  • 732
  • 636
0
function Clear (x) {if (x.cleared) {} else {x.value = ""; x.cleared = true}}

onfocus = "Clear (this)"
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
  • Please add some explanation. – Nikolay Kostov Apr 20 '15 at 07:45
  • I really can't see the point; -- either the code is transparently obvious or the reader is not yet sufficiently well informed to be able to make use of it. It's intended as an answer, not as a tutorial. – Philip Taylor Apr 20 '15 at 07:57
  • A few words of explanation would make this example even better. – Thom Apr 20 '15 at 12:12
  • "Clear" takes an object as parameter, tests whether that object has a property "cleared" with Boolean value (indicating that the value of the object has already been cleared -- the cleared state cannot exist /ab initio/) and if it does NOT have that property, clears the value of the object and sets the cleared property to . "onfocus" allows an event handler associated with (in this case) a text-input field to be specified; here the event handler is specified to be an invocation of the "Clear" function, passing as parameter the object corresponding to the text-input field. – Philip Taylor Apr 21 '15 at 07:21
0

Add a following script to your js file:

var input1 = document.getElementById("input1")

input1.onfocus = function() {
  if(input1.value == "Enter Postcode or Area") {
      input1.value = "";
  } 
};

input1.onblur = function() {
    if(input1.value == "") {
       input1.value = "Enter Postcode or Area";
   } 
 };
Matyas
  • 21
  • 6