July 19, 201115 yr 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
July 19, 201115 yr 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 July 19, 201115 yr by ysuran
July 19, 201115 yr 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.
July 19, 201115 yr 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 Edited July 19, 201115 yr by ysuran
July 19, 201115 yr 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