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:

#!/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;
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!
<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

In reply to Using labelattributes with CGI's checkbox_group by richardwfrancis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.