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

How can you override the width of a popup_menu using CGI? Length and Width are not available functions and Size is a vertical enhancement. Without using CSS or another module, is it possible?
Tr(td("Title: "), td(popup_menu(-name=>'distribution', -values=>['','value','value1']..

Replies are listed 'Best First'.
Re: CGI form element sizing issue
by cLive ;-) (Prior) on Apr 04, 2003 at 06:21 UTC
    Pad the labels with spaces (and possibly   spaces).

    .02

    cLive ;-)

      Only problem I see with that is if you add spaces to the value to line them up then when you print out the params there will be lots of spaces at the end.

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
        No, I said the labels - the values are not affected.
        $q->scrolling_list(-name =>'list_name', -values =>['eenie','meenie','minie','moe'], -default=>['eenie','moe'], -size =>1, -labels => { eenie => ' eenie ', meenie => ' meenie ', + minie => ' minie ', mo => ' mo ' } );

        cLive ;-)

Re: CGI form element sizing issue
by benn (Vicar) on Apr 04, 2003 at 08:44 UTC
    Sorry, but Clive hit the nail on the head. The width of the popup menu is decided by the browser, not by the HTML /CSS, based upon the length of the rendered strings. I tend to use substr() with an indicative ellipsis ('...') if I've got a bunch of strings that are too long to fit in the allocated space.
    Cheers, Ben.
they've all got CSS in them but the last example doesn't need it
by Your Mother (Archbishop) on Apr 04, 2003 at 14:14 UTC
    use warnings; use strict; use CGI::Pretty qw(:standard); # plain CGI will do too #========================================== my %id_to_term = ( 92 => 'Tuna', 3 => 'Ceolocanth', 4 => 'Ratfish', 7 => 'Six Gill Shark', 13 => 'Albino Pygmy Sperm Whale', ); print header(), start_html(), start_form(); print h3("Regular"), popup_menu( -labels => \%id_to_term, -values => [ sort keys %id_to_term ], ); print h3("Shortened"), popup_menu( -style => "font-size:75%;", -labels => \%id_to_term, -values => [ sort keys %id_to_term ], ); print h3("Moreso"), popup_menu( -style => "font-size:60%;", -labels => \%id_to_term, -values => [ sort keys %id_to_term ], ); # now we modify the values with word_chop since we're able to look up # by the IDs anyway, it doesn't matter that we modify them my %id_to_chopped_term = %id_to_term; while ( my ( $id, $term ) = each %id_to_chopped_term ) { $id_to_chopped_term{$id} = word_chop($term, 12); } print h3("Okay, that's ridiculous"), popup_menu( -style => "font-size:50%;", -labels => \%id_to_chopped_term, -values => [ sort keys %id_to_chopped_term ], ); print end_form(), end_html(); exit 0; #=========================================== sub word_chop { my ( $text, $chars ) = @_; $chars ||= 15; return $text if $chars >= length $text; return substr($text, 0, $chars) . '...'; }
Re: CGI form element sizing issue
by Chady (Priest) on Apr 04, 2003 at 06:28 UTC
    This works:
    popup_menu(-name=>'distribution', -size=>20, -values=>['','value','value1'...
    From perldoc CGI
    Any additional parameters you provide, such as the Netscape unofficial BGCOLOR attribute, are added to the <BODY> tag.
    And I think it applies to every element generated with CGI.pm

    Update: misunderstodd the question. :D


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/