Clear all forms on page load
Asked Answered
T

3

6

I want to clear all forms on page load. I tried to use this function on domready, but it doesn't help. I'm new to JavaScript. Is there anything wrong with this function?

   $(':input', form)
 .not(':button, :submit, :reset, :hidden')
 .val('')
 .removeAttr('checked')
 .removeAttr('selected');
Travax answered 17/11, 2011 at 23:28 Comment(0)
A
14

You can try using the plain javascript reset method on a form

$('form').each(function() { this.reset() });

This should reset each form to its default state.

To re-enable all checkboxes, you can try:

$(':checkbox').prop('disabled', false);
Avan answered 17/11, 2011 at 23:35 Comment(4)
I have other function that limits checkbox amount in 1 of forms. I tried this code within domready. But after refresh i see that there is no selected checkbox BUT ALL PREVIOUSLY DISABLED (BY jQ SCRIPT) CHECKBOXES ARE STILL DISABLED $("input[type=checkbox][name=subjects]").click(function() { var bol = $("input[type=checkbox][name=subjects]:checked").length >= 3; $("input[type=checkbox][name=subjects]").not(":checked").attr("disabled",bol); });Travax
@TuralTeyyuboglu It sounds like you will need to write a custom reset method that will undo these modifications.Avan
Then help me to reset and re-enable theese checkboxesTravax
Finally:)) Thx code looks like that: $('form').each(function() { this.reset() }); $(':checkbox').prop('disabled', false); Edit your answer if you want. Also upvote my Q ))Travax
W
2

maybe this is what your asking? not sure why you would need it. the fields should be blank on page load anyways. you should change the values php side.

$('input[type=text]').val('');
$('input[type=radio]').checked=false;
$('input[type=checkbox]').checked=false;

or maybe even

$("input:not(':button, :submit, :reset, :hidden')").val('').checked=false;
Wolgast answered 17/11, 2011 at 23:32 Comment(2)
Maybe text inputs always clear, but radio button, checkboxes not. That's why I wanna reset all forms on page loadTravax
I have other function that limits checkbox amount in 1 of forms. I tried this code within domready. But after refresh i see that there is no selected checkbox BUT ALL PREVIOUSLY DISABLED (BY jQ SCRIPT) CHECKBOXES ARE STILL DISABLED $("input[type=checkbox][name=subjects]").click(function() { var bol = $("input[type=checkbox][name=subjects]:checked").length >= 3; $("input[type=checkbox][name=subjects]").not(":checked").attr("disabled",bol); });Travax
C
0

I would give each control i wanted to clear a class name of say class="ClearOnStartup" and then my jQuery would be;

$(function(){
  $(".ClearOnStartup").val("");
});

I'd have a different one for check boxes only because I like to seperate things like this into batches.

try this for checkboxes

$('.ClearOnStartup').attr('checked', false);

There is probably a better way tho

Chatterer answered 17/11, 2011 at 23:31 Comment(1)
i don't want to add extra class just because of form clear. There must be other way,Travax

© 2022 - 2024 — McMap. All rights reserved.