ShoeLace1291 Posted February 5, 2010 Share Posted February 5, 2010 I need to be able to set the values of a checkbox if it's unchecked. I have an array of checkboxes that I'm looping through in PHP so the values of all unchecked boxes in the form need to be set to 0(zero). How would I do this with javascript? Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/ Share on other sites More sharing options...
trq Posted February 5, 2010 Share Posted February 5, 2010 Unchecked checkboxes are not submitted, is this your problem? Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1007230 Share on other sites More sharing options...
ShoeLace1291 Posted February 5, 2010 Author Share Posted February 5, 2010 Yes. When the form is submitted, I need the script to find all checkboxes that are unchecked and set their value to zero. Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1007233 Share on other sites More sharing options...
trq Posted February 5, 2010 Share Posted February 5, 2010 Easy enough to do with jquery, something like.... $(document).ready(function() { $('#submittbn').submit(function() { $("input[type='checkbox']").not(':checked')).each(function() { $this.val(0).attr('checked', true); }); }); }); Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1007234 Share on other sites More sharing options...
ShoeLace1291 Posted February 5, 2010 Author Share Posted February 5, 2010 I've never used JQuery, or I would figure it out on my own, but I'm getting syntax errors in firebug's error console. Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1007250 Share on other sites More sharing options...
RussellReal Posted February 5, 2010 Share Posted February 5, 2010 jQuery is a library created by someone, you need to get the jQuery js file but you can do it without jQuery, and probably with the same amount of code Edit: Well, probably a couple lines more, but thats not the reason for my edit.. Thorpe's example (which is probably the only way to do it anyway.. minus the jquery) would set checked attribute to true, which will literally tick the checkbox you'd want to ALSO set the value of the checkbox to 0.. just a sidenote Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1007260 Share on other sites More sharing options...
trq Posted February 5, 2010 Share Posted February 5, 2010 you'd want to ALSO set the value of the checkbox to 0 Which is what this bit.... $this.val(0) does. Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1007265 Share on other sites More sharing options...
RussellReal Posted February 5, 2010 Share Posted February 5, 2010 you'd want to ALSO set the value of the checkbox to 0 Which is what this bit.... $this.val(0) does. wow jquery does it all huh.. so val() actually returns the updated element after the value change? I personally never used jQuery Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1007269 Share on other sites More sharing options...
trq Posted February 5, 2010 Share Posted February 5, 2010 Most jQuery methods return a jQuery object so you can chain methods together easily. Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1007288 Share on other sites More sharing options...
ShoeLace1291 Posted February 6, 2010 Author Share Posted February 6, 2010 can someone be so kind as to make a code for me? i only know a little bit of javascript or else i would make it myself. Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1007768 Share on other sites More sharing options...
trq Posted February 6, 2010 Share Posted February 6, 2010 I already did. Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1007777 Share on other sites More sharing options...
ShoeLace1291 Posted February 7, 2010 Author Share Posted February 7, 2010 it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1008585 Share on other sites More sharing options...
gizmola Posted February 7, 2010 Share Posted February 7, 2010 jQuery is a library created by someone, you need to get the jQuery js file but you can do it without jQuery, and probably with the same amount of code Edit: Well, probably a couple lines more, but thats not the reason for my edit.. Thorpe's example (which is probably the only way to do it anyway.. minus the jquery) would set checked attribute to true, which will literally tick the checkbox you'd want to ALSO set the value of the checkbox to 0.. just a sidenote Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1008587 Share on other sites More sharing options...
RussellReal Posted February 8, 2010 Share Posted February 8, 2010 jQuery is a library created by someone, you need to get the jQuery js file but you can do it without jQuery, and probably with the same amount of code Edit: Well, probably a couple lines more, but thats not the reason for my edit.. Thorpe's example (which is probably the only way to do it anyway.. minus the jquery) would set checked attribute to true, which will literally tick the checkbox you'd want to ALSO set the value of the checkbox to 0.. just a sidenote ily man Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1008593 Share on other sites More sharing options...
ShoeLace1291 Posted February 8, 2010 Author Share Posted February 8, 2010 I downloaded the jquery file and uploaded it to my server. still doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1008666 Share on other sites More sharing options...
RussellReal Posted February 8, 2010 Share Posted February 8, 2010 now you need to include it.. <script type="text/javascript" src="jQuery.js"></script> Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1008681 Share on other sites More sharing options...
trq Posted February 8, 2010 Share Posted February 8, 2010 Put this just before your </body> tag. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script> My code is also untested so it might need some debugging in firebug. Quote Link to comment https://forums.phpfreaks.com/topic/191016-set-values-of-unchecked-checkboxes/#findComment-1008691 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.