I'm trying to color a menu of items on an webpage, based on the item currently selected. I know the item chosen, based on $cgi->param('a'), so that part isn't a problem.

I've abstracted the logic into a small, standalone script that looks like this:

my @menu_options = ('home', 'donate', 'news', 'docs', 'download', 'gal +lery', 'samples', 'users', 'developers', 'about'); my $items = @menu_options; my $count = 1; print qq{ <div class="sh"> <span class="doNotDisplay">Navigation:</span> }; my $style = ''; foreach my $menu_item (@menu_options) { $style = qq{style="background-color: #9943ac;"} if ($action eq $menu_item && $action =~ /docs/); $style = qq{style="background-color: #d8402b;"} if ($action eq $menu_item && $action =~ /download/); $style = qq{style="background-color: #ab0;"} if ($action eq $menu_item && $action =~ /users/); $style = qq{style="background-color: #6fab87;"} if ($action eq $menu_item && $action =~ /developers/); print qq{\t\t<a href="/$menu_item" $style>$menu_item</a>}; print " |" if ($count < $items); print "\n"; $count++; } print "\t</div>\n";

In my actual code, I have a dispatch table that looks like this:

my %dispatch = ( (map { $_ => \&default } qw( home donate news docs download gallery docs users developers about )), samples => sub { \&samples($dbh) }, );

I'm trying to selectively color the background of the item selected. What am I missing in my sample code above?

I'm only trying to colorize 4 of the 10 menu options, and only one should be colorized at a time. They're in my dispatch table in the production code (not an array like the sample test above; I created that to try to isolate the problem).

Is there a better way to do this?


In reply to Selectively coloring a menu's item members by hacker

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.