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

use CGI; $co = new CGI; print $co->header; print $co->start_html(); print $co->start_form; print $co->radio_group( -name => 'color', -id => 'color', -values => ['red','yellow','green'], -default => 'red', -onClick => "return select_color()" ).$co->p; #How to handle $color below so that whatever color i select with radio +button that color is displayed in textarea?? print $co->textarea( -style=>"background-color:$color; color:blue", ); print $co->end_form; print $co->end_html; print <<COLOR_SCRIPT; <script language="JavaScript"> function select_color() { var color = document.getElementById('color').value; return color; } </script> COLOR_SCRIPT
In the above program, I want background color of textarea to be changed according to the radiobutton selected.

Replies are listed 'Best First'.
Re: COLOR of textarea
by aaron_baugher (Curate) on Nov 14, 2011 at 12:37 UTC

    You're getting your server stuff confused with your client stuff. Anything that happens dynamically on the page after it's loaded will be done by Javascript. The Perl script that creates the page will need to print that Javascript along with the HTML of the page. (Or it could be in a separate .js file that's included by the page.) So figure out what your Javascript needs to be by creating a static HTML test page with radio buttons that do what you want; then when that works, have your CGI print what you came up with.

    I would highly recommend that you look into jQuery. It's a framework for doing dynamic page manipulation that sits on top of Javascript and eliminates a lot of the frustration of the language. This kind of thing is a one-liner in jQuery.

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

Re: COLOR of textarea
by marto (Cardinal) on Nov 14, 2011 at 10:37 UTC
Re: COLOR of textarea
by Anonymous Monk on Nov 14, 2011 at 09:42 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: COLOR of textarea
by locked_user sundialsvc4 (Abbot) on Nov 14, 2011 at 14:50 UTC

    Yes, indeed.   Anything that happens “in response to a user-interface action on the user side” always is handled by JavaScript ... on the user side.   (The server does not become involved at all, unless the JavaScript initiates an AJAX request.)

    The best-all-around way to handle appearance changes like this, in my humble opinion, is with CSS styles.   Your JavaScript code can query the object to determine which style it currently uses, remove that style and add the “next” one.   This allows you to change many characteristics of the object at one time and to do so consistently throughout.   There are plenty of examples on the Web on how to do this, and none of them involve Perl.

Re: COLOR of textarea
by liverpole (Monsignor) on Nov 14, 2011 at 18:10 UTC
    Hi tarunkhanna,

    As other have noted, this hasn't anything to do with perl.

    But since you're pretty close, try doing this:

    #!/usr/bin/perl -w use strict; use warnings; use CGI; my $co = new CGI; my $color = 'red'; print $co->header; print $co->start_html(); print $co->start_form; print $co->radio_group( -name => 'color', -id => 'color', -values => ['red','yellow','green'], -default => 'red', -onClick => "return select_color(this)" ).$co->p; print $co->textarea( -id => "textarea", -style=> "background-color:$color; color:blue", ); print $co->end_form; print $co->end_html; print <<COLOR_SCRIPT; <script language="JavaScript"> function select_color(rb_obj) { var color = rb_obj.value; var ta_obj = document.getElementById('textarea'); ta_obj.style.background = color; return color; } </script> COLOR_SCRIPT

    Note that it passes "this" to the function select_color, so that the value will change with the color. The id of the text_area object is then looked up, and its style.background is assigned to the current color.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/