How can I clear the input text after clicking

Jquery

Jquery Problem Overview


Using jQuery, how can I clear the input text after I made a click? By default, the value remains in the input field.

For example, I have an input text and the value is TEXT. When I perform a click, I want the input field to become empty.

Jquery Solutions


Solution 1 - Jquery

To remove the default text, on clicking the element:

$('input:text').click(
    function(){
        $(this).val('');
    });

I would, though, suggest using focus() instead:

$('input:text').focus(
    function(){
        $(this).val('');
    });

Which responds to keyboard events too (the tab key, for example). Also, you could use the placeholder attribute in the element:

<input type="text" placeholder="default text" />

Which will clear on focus of the element, and reappear if the element remains empty/user doesn't input anything.

Solution 2 - Jquery

Update: I took your saying "click" literally, which was a bit dumb of me. You can substitute focus for click in all of the below if you also want the action to happen when the user tabs to the input, which seems likely.

Update 2: My guess is that you're looking to do placeholders; see note and example at the end.


Original answer:

You can do this:

$("selector_for_the_input").click(function() {
    this.value = '';
});

...but that will clear the text regardless of what it is. If you only want to clear it if it's a specific value:

$("selector_for_the_input").click(function() {
    if (this.value === "TEXT") {
        this.value = '';
    }
});

So for example, if the input has an id, you could do:

$("#theId").click(function() {
    if (this.value === "TEXT") {
        this.value = '';
    }
});

Or if it's in a form with an id (say, "myForm") and you want to do this for every form field:

$("#myForm input").click(function() {
    if (this.value === "TEXT") {
        this.value = '';
    }
});

You may also be able to do it with delegation:

$("#myForm").delegate("input", "click", function() {
    if (this.value === "TEXT") {
        this.value = '';
    }
});

That uses delegate to hook up a handler on the form but apply it to the inputs on the form, rather than hooking up a handler to each individual input.


If you're trying to do placeholders, there's more to it than that and you may want to find a good plug-in to do it. But here's the basics:

HTML:

<form id='theForm'>
  <label>Field 1:
    <input type='text' value='provide a value for field 1'>
  </label>
  <br><label>Field 2:
    <input type='text' value='provide a value for field 2'>
  </label>
  <br><label>Field 3:
    <input type='text' value='provide a value for field 3'>
  </label>
</form>

JavaScript using jQuery:

jQuery(function($) {

  // Save the initial values of the inputs as placeholder text
  $('#theForm input').attr("data-placeholdertext", function() {
    return this.value;
  });
  
  // Hook up a handler to delete the placeholder text on focus,
  // and put it back on blur
  $('#theForm')
    .delegate('input', 'focus', function() {
      if (this.value === $(this).attr("data-placeholdertext")) {
        this.value = '';
      }
    })
    .delegate('input', 'blur', function() {
      if (this.value.length == 0) {
        this.value = $(this).attr("data-placeholdertext");
      }
    });
  
});

Live copy

Of course, you can also use the new placeholder attribute from HTML5 and only do the above if your code is running on a browser that doesn't support it, in which case you want to invert the logic I used above:

HTML:

<form id='theForm'>
  <label>Field 1:
    <input type='text' placeholder='provide a value for field 1'>
  </label>
  <br><label>Field 2:
    <input type='text' placeholder='provide a value for field 2'>
  </label>
  <br><label>Field 3:
    <input type='text' placeholder='provide a value for field 3'>
  </label>
</form>

JavaScript with jQuery:

jQuery(function($) {
  
  // Is placeholder supported?
  if ('placeholder' in document.createElement('input')) {
    // Yes, no need for us to do it
    display("This browser supports automatic placeholders");
  }
  else {
    // No, do it manually
    display("Manual placeholders");

    // Set the initial values of the inputs as placeholder text
    $('#theForm input').val(function() {
      if (this.value.length == 0) {
        return $(this).attr('placeholder');
      }
    });
    
    // Hook up a handler to delete the placeholder text on focus,
    // and put it back on blur
    $('#theForm')
      .delegate('input', 'focus', function() {
        if (this.value === $(this).attr("placeholder")) {
          this.value = '';
        }
      })
      .delegate('input', 'blur', function() {
        if (this.value.length == 0) {
          this.value = $(this).attr("placeholder");
        }
      });
  }
  
  function display(msg) {
    $("<p>").html(msg).appendTo(document.body);
  }
  
});

Live copy

(Kudos to diveintohtml5.ep.io for the placholder feature-detection code.)

Solution 3 - Jquery

$("input:text").focus(function(){$(this).val("")});

Solution 4 - Jquery

You could try this

$('#myFieldID').focus(function(){
  $(this).val('');
});

Solution 5 - Jquery

I am supposing you are trying to create a effect, where the textbox contains a label. And when the user click in the textbox, it disappears and lets the user input the text. You do not require Jquery for this.

<input type="text" value="Input Text" onfocus="this.value=''" onblur="(this.value=='')? this.value='Input Text':this.value;" />

Demo

Solution 6 - Jquery

This worked for me:

    **//Click The Button**    
    $('#yourButton').click(function(){

         **//What you want to do with your button**
         //YOUR CODE COMES HERE

         **//CLEAR THE INPUT**
         $('#yourInput').val('');

});

So first, you select your button with jQuery:

$('#button').click(function((){ //Then you get the input element $('#input')
    //Then you clear the value by adding:
    .val(' '); });

Solution 7 - Jquery

Using jQuery ...

$('#submitButtonsId').click(
    function(){
        $(#myTextInput).val('');
    });

Using pure Javascript ...

var btn = document.getElementById('submitButton');
btn.onclick = function(){ document.getElementById('myTextInput').value="" };

Solution 8 - Jquery

There is an elegant way to do this:

<input type="text" value="Search" name="" 
       onfocus="if (this.value == 'Search') {this.value = '';}" 
       onblur="if (this.value == '') {this.value = 'Pesquisa';}"/>

In this way, if you type anything inside the search box, it won't be deleted when click inside the box again.

Solution 9 - Jquery

> >

>
> First Name: >
> Last Name: >
> >
> >

Solution 10 - Jquery

If you need placeholder like behavior. you can use this.

$("selector").data("DefaultText", SetYourDefaultTextHere);
// You can also define the DefaultText as attribute and access that using attr() function

$("selector").focus(function(){
if($(this).val() == $(this).data("DefaultText"))
   $(this).val('');
});

Solution 11 - Jquery

try this

$("input[name=search-mini]").on("search", function() {
 //do something for search
});

Solution 12 - Jquery

I would recommend to use this since I have the same issue which got fixed.

$('input:text').focus(
    function(){
        $(this).val('');
    });

Solution 13 - Jquery

  $('.mybtn').on('click',
      function(){
          $('#myinput').attr('value', '');
      }
  );

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <button class="mybtn">CLEAR</button>
  <input type="text" id="myinput" name="myinput" value="ааааааааа">

Solution 14 - Jquery

    enter code here<form id="form">
<input type="text"><input type="text"><input type="text">

<input type="button" id="new">
</form>
<form id="form1">
<input type="text"><input type="text"><input type="text">

<input type="button" id="new1">
</form>
<script type="text/javascript">
$(document).ready(function(e) {
    $("#new").click( function(){
		//alert("fegf");
	$("#form input").val('');
	});
	
	  $("#new1").click( function(){
		//alert("fegf");
	$("#form1 input").val('');
	});
});
</script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPsyGnosisView Question on Stackoverflow
Solution 1 - JqueryDavid ThomasView Answer on Stackoverflow
Solution 2 - JqueryT.J. CrowderView Answer on Stackoverflow
Solution 3 - JqueryRyanView Answer on Stackoverflow
Solution 4 - JqueryscunliffeView Answer on Stackoverflow
Solution 5 - JqueryStarxView Answer on Stackoverflow
Solution 6 - JqueryMichiel J OttoView Answer on Stackoverflow
Solution 7 - JqueryAkbar MirsiddikovView Answer on Stackoverflow
Solution 8 - JqueryPedro OliveiraView Answer on Stackoverflow
Solution 9 - JqueryHrushiView Answer on Stackoverflow
Solution 10 - JqueryBharathView Answer on Stackoverflow
Solution 11 - JqueryDon ZanuranoView Answer on Stackoverflow
Solution 12 - Jqueryuser7031310View Answer on Stackoverflow
Solution 13 - JqueryMaXXView Answer on Stackoverflow
Solution 14 - JqueryramiView Answer on Stackoverflow