http://www.perlmonks.org?node_id=635553


in reply to Howto Include JavaScript to CGI.pm Script

Hello again,
you also didn't set the onClick attribute of '2 Fastest'.
I've modified the javascript slightly since you checked 2 Fastest by default. Clicking on any other checkbox will uncheck 2 Fastest now.

Depending on what you are going to do I'd possibly like to recommend some templating system like e.g. HTML::Template

For debugging javascript I use firebug with firefox. www.getfirebug.com

Greetings, michael
#!/usr/bin/perl -w use strict; use diagnostics; use Data::Dumper; use CGI qw/:standard :html3/; use CGI::Carp qw( fatalsToBrowser ); my $JSCRIPT=<<EOF; var twofastest; var checkboxes=[]; function initVars(){ if ( twofastest ) return; twofastest = document.getElementById('2 Fastest'); var cb = document.getElementsByName('progname'); var b = 0; for (var a=0; a<cb.length; a++ ){ if ( cb[a] != twofastest ){ checkboxes[b] = cb[a]; b++; } } } function checkBoxClick(){ initVars(); var anyboxchecked = false; for (var a=0; a<checkboxes.length; a++ ){ if ( checkboxes[a].checked ) anyboxchecked = true; } if ( anyboxchecked ) twofastest.checked = false; twofastest.disabled = anyboxchecked; } function twofastestClick(){ initVars(); for (var a=0; a<checkboxes.length; a++ ){ if ( twofastest.checked ) checkboxes[a].checked = false; checkboxes[a].disabled = twofastest.checked; } } EOF ; print header, start_html( -title => "Some Title", -script=> $JSCRIPT ); # some portions of code sub generate_form { print start_multipart_form(), #strong('Your email: '), #textfield( -name => 'user_email' ), br, br strong('Basic Programs To Choose: '), br checkbox_group( -name => 'progname', -values => [ '2 Fastest', 'MEME', 'MITRA', 'SPACE', ], -attributes=>{'2 Fastest'=>{'id'=>'2 Fastest', 'onClick'=>'two +fastestClick()'}, MEME=>{onClick=>'checkBoxClick()' }, MITRA=>{onClick=>'checkBoxClick()' }, SPACE=>{onClick=>'checkBoxClick()' } }, #the extra line added to print id -rows => '2', -columns => '6', -defaults => [ '2 Fastest' ], ), p, } generate_form(); print end_html();