Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Onload clear all text fields jquery js

How do I clear all text fields on a page, deselect buttons etc? I simply want to refresh the page when it loads.. like holding Shift + Refresh?

like image 850
Daniel Kellaway Avatar asked Dec 19 '25 18:12

Daniel Kellaway


2 Answers

Give this a try:

$(document).ready(function() {
    $('input[type!="button"][type!="submit"], select, textarea')
         .val('')
         .blur();
});

Clearing the values on buttons and submit buttons will give them a blank label, hence the more complex selector.

like image 163
Bojangles Avatar answered Dec 21 '25 06:12

Bojangles


If all elements are in form,

then use document.getElementById('your-form-id').reset();

or $('#your-form-id')[0].reset();

Otherwise:

$('input, textarea').each(function() {
    this.value = '';
});
like image 36
Eugene Naydenov Avatar answered Dec 21 '25 07:12

Eugene Naydenov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!