Friday 13 July 2012

How to clear textbox value using jQuery

In this short post, I will show you simple jQuery code to clear textbox value. The reason for writing this post is because I have seen many beginner programmers using val() == '', which is wrong as val() is a method that takes argument. It is not a property or attribute. You can call this code on click of button or any other event.

Clear all textbox

$(document).ready(function() {

    $('input[type=text]').each(function() {

        $(this).val('');

    });

});​
Clear single textbox value

$(document).ready(function() {

   $('#txtID').val(''); //txtID is textbox ID

});​
Clear textbox value onfocus

$(document).ready(function() {

   $('#txtID').focus(function() {

        $(this).val('');

    });

});​
Associate focus event with every textbox

1$(document).ready(function() {
2    $('input[type=text]').focus(function() {
3        $(this).val('');
4    });
5});​

No comments:

Post a Comment

What should you required to learn machine learning

  To learn machine learning, you will need to acquire a combination of technical skills and domain knowledge. Here are some of the things yo...