Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Clear Form(s) problem

Featured Replies

Hey,

 

Basically I have a website, that when loads has a couple of buttons (images) which you can click. Once one is clicked, I use some jQuery to show the contents of a div below. And then if the other button is clicked, is hides the one thats currently open and replaces it with another, different div.

 

the jQuery:

 

			<script type="text/javascript">	
		$(document).ready(function() {
			$('#css-div').hide();
			$('#tf2-div').hide();

			 	$('#servers-1').click(function() {
   					$('#tf2-div').show('slow');
					$('#css-div').hide('slow');
   					return false;
 					});

				$('#servers-2').click(function() {
   					$('#tf2-div').hide('slow');
					$('#css-div').show('slow');
   					return false;
 					});
		});
		</script>

 

Now inside of these hidden divs is a form (each div has its own form). This form ONLY contains checkboxes.

 

The thing I am trying to do and I fail with my JavaScript is that when either of the buttons at the top are clicked, all checkboxes in both forms are cleared.

 

However, I dont want to use the clear button within the form as these buttons are outside of the form. So I was wondering if there was any kind of:

 

onClick="clear these forms" function.

 

Cheers

cant you use something similar to this:

 

$('#Button').click(function(){
$("form[name=formname] :input").each(function(){
	switch(this.type) {
		case 'password':case 'select-multiple':case 'select-one':case 'text':case 'textarea':
			$(this).val('');
			break;
		case 'checkbox':case 'radio':
			$(this).removeAttr('checked');
	}
});
});

 

where #button is the id of the button you want the onclick to work on and formname is the name of the form you want cleared.

 

I've not tested this out but its just a tweaked version of the code I use to clear my forms and should clear any form value.

Hope that helps :)

Edited by ysuran

If each form has a unique ID e.g.

 

<form id="forma" ...

<form id="formb" ...

 

...then you could do something like:

document.getElementById('forma').reset();

document.getElementById('formb').reset();

...to reset each form.

haha cascade is right sorry i've just realised mine avoids resetting my hidden inputs I use for form validation!!

 

so jquery version is simply:

 

$('#formId').reset()

 

Sorry :fool:

Edited by ysuran

  • Author

Oh nice :) Didnt realise it was that easy, all the JavaScript versions about call big functions and stuff which got me confused D:

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.