Search code examples
javascriptperlcgidynamic-html

How do I enable a submit button after a radio button is selected?


I am writing a Perl CGI script.

At first the submit button is disabled, it should be enabled when any of the radio button is selected. A radio button exists for each corresponding row (same name).


Solution

  • This is more probably a JavaScript question, unless you wan to reload the page with a submit button enabled. But you can specify JavaScript events in CGI.

    For a radio group, it's just a little trickier. Since the radio_group sub allows attributes per item, you just have to specify the same event for each selection.

    my @values = qw<eenie meenie minie>;
    my $enableSubmit 
        = { -onClick => q[document.getElementById('mySubmitButton').disabled = false;] }
        ;
    print CGI->radio_group(
          -name       => 'group_name'
        , -values     => \@values
        , -default    => 'meenie'
        , -linebreak  => 'true'
        , -labels     => { map { $_ => ucfirst } @values }
        , -attributes => { map { $_ => $enableSubmit } @values } 
        );