backedge has asked for the wisdom of the Perl Monks concerning the following question:

I have a need to produce tooltip help on each option that appears in a popup_menu in CGI.pm. I know this is possible by adding a title attribute to each option, so the browser shows this when the mouse hovers over it. I can't figure out how to do it within popup_menu.

Here's a code snippet that works using Select (deprecated) instead of popup_menu:

print $q->start_Select({ -id => 'idprd_order_allowed', -name => 'prd_order_allowed', -override => 1, -onChange => "changeInCore=true", -default => $prd_order_allowed }); foreach my $option (@low_values_prd_order_allowed){ print $q->option({ -value=>"$option", -title=>"$titles_prd_order_allowed{$option}"}, "$meanings_prd_order_ +allowed{$option}"); } print $q->end_Select();

I want to replicate that -title=> behaviour in popup_menu() - currently have:

print $q->popup_menu( -id => 'idprd_order_allowed', -name => 'prd_order_allowed', -override => 1, -onChange => "changeInCore=true", -default => $prd_order_allowed, -value => [@low_values_prd_order_allowed], -labels => \%meanings_prd_order_allowed );

I'm sure (I hope!) there is a simple option similar to -labels that I am missing. Any suggestions?

Replies are listed 'Best First'.
Re: Adding attributes to select list options in CGI.pm popup_menu?
by Anonymous Monk on Jun 30, 2011 at 12:06 UTC

    I want to replicate that -title=> behaviour in popup_menu()

    The manual is very clear http://search.cpan.org/dist/CGI/lib/CGI.pm#CREATING_A_POPUP_MENU

    #!/usr/bin/perl -- use CGI; print CGI->popup_menu( -values => ['a', 'b'], -labels => {'a', 'labela', 'b', 'labelb'}, -attributes => { 'a' => { title => 'titlea' }, 'b' => { title => 'titleb' }, }, ); __END__ <select name="" > <option title="titlea" value="a">labela</option> <option title="titleb" value="b">labelb</option> </select>

      My problem was actually in populating my %titles_prd_order_allowed, which I didn't include, trying to keep it short - sorry. Sorted now, thanks.

      my @low_values_prd_order_allowed = (); my %meanings_prd_order_allowed = (); my %titles_prd_order_allowed = (); my $stmt = "SELECT value, meaning, title FROM OPTIONS "; my $sth = $dbh->prepare($stmt); $sth->execute(); while ( ( $value, $meaning, $title ) = $sth->fetchrow_array() ) { push( @low_values_prd_order_allowed, $value ); $meanings_prd_order_allowed{$value} = $meaning; $titles_prd_order_allowed{$value} = ( $value =>{'title'=> $title} ); } print $q->label( { -for => 'idprd_order_allowed' }, "ORDER ALLOWED" ); print $q->popup_menu( -id => 'idprd_order_allowed', -name => 'prd_order_allowed', -override => 1, -onChange => "changeInCore=true", -default => $prd_order_allowed, -value => [@low_values_prd_order_allowed], -labels => \%meanings_prd_order_allowed, -attributes => \%titles_prd_order_allowed );

      I (being a numpty) previously had:

      $titles_prd_order_allowed{$rv_low_value} = $title;