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
Clear single textbox value
Clear textbox value onfocus
Associate focus event with every textbox
Clear all textbox
| $(document).ready( function () { |
| $( 'input[type=text]' ).each( function () { |
| $( this ).val( '' ); |
| }); |
| }); |
| $(document).ready( function () { |
| $( '#txtID' ).val( '' ); //txtID is textbox ID |
| }); |
| $(document).ready( function () { |
| $( '#txtID' ).focus( function () { |
| $( this ).val( '' ); |
| }); |
| }); |
1 | $(document).ready( function () { |
2 | $( 'input[type=text]' ).focus( function () { |
3 | $( this ).val( '' ); |
4 | }); |
5 | }); |
No comments:
Post a Comment