richardwfrancis has asked for the wisdom of the Perl Monks concerning the following question:
Hello wise ones,
I'm having some trouble with CGI's checkbox_group function and more specifically the ability to assign attributes to the <label> tag using "-labelattributes"
Using this code for a single checkbox assigns the label with the css class as requested:
#!/usr/bin/perl use strict; use CGI qw/:standard/; my $cgi = new CGI; print $cgi->header; print $cgi->start_html; print $cgi->checkbox(-name => 'my_checkbox', -value => 'eenie', -label => 'first choice', -labelattributes => { class => 'somecssclass' } ); print $cgi->end_html;
When taking this to a checkbox_group, all labels appear with the same class:
#!/usr/bin/perl use strict; use CGI qw/:standard/; my $cgi = new CGI; print $cgi->header; print $cgi->start_html; my %labels = ( 'eenie'=>'first choice', 'meenie'=>'second choice', 'minie'=>'third choice'); print $cgi->checkbox_group(-name => 'my_checkbox', -values => ['eenie','meenie','minie'], -labels => \%labels, -labelattributes => { class => 'somecssclass' } ); print $cgi->end_html;
What I want to do is similar to the way "-attributes" works, so as to be able to control *which* labels I assign attributes to. Something like:
However when I do this I get the following, which given the previous code I kind of expected but don't really want! Note that the <input> tag for the value 'eenie' gets assigned the right class but ALL labels get assigned some unwanted eenie=hashref garbage!#!/usr/bin/perl use strict; use CGI qw/:standard/; my $cgi = new CGI; print $cgi->header; print $cgi->start_html; my %labels = ('eenie'=>'first choice', 'meenie'=>'second choice', 'minie'=>'third choice'); my %labelattr = ('eenie'=> { 'class' => 'somecssclass' } ); my %attr = ('eenie'=> { 'class' => 'someothercssclass' } ); print $cgi->checkbox_group(-name => 'my_checkbox', -values => ['eenie','meenie','minie'], -labels => \%labels, -attributes => \%attr, -labelattributes => \%labelattr ); print $cgi->end_html;
<label eenie="HASH(0x13e95e8)"><input type="checkbox" name="my_checkbo +x" value="eenie" class="someothercssclass"/>first choice</label><lab +el eenie="HASH(0x13e95e8)"><input type="checkbox" name="my_checkbox" +value="meenie" />second choice</label><label eenie="HASH(0x13e95e8)"> +<input type="checkbox" name="my_checkbox" value="minie" />third choic +e</label>
Is there any way to specifically assign attributes to specific labels using cgi and checkbox_group?
Many many thanks for your help.
Rich
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using labelattributes with CGI's checkbox_group
by Anonymous Monk on Feb 26, 2011 at 08:27 UTC | |
by richardwfrancis (Beadle) on Feb 26, 2011 at 09:59 UTC |