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

Hi, please look at the following code.
$string .= $cgi->textfield( -name => 'name', -default => 'def', -override => 0, -size => 20, -maxlength => 120, -onfocus => "this.value = ''", -onblur => "this.value = 'def'", -onchange => "delete onfocus, delete onblur", #how? );
I want to "unset/remove/delete" the onfocus and onblur attributes if there was a change.
Thank you very much.

Replies are listed 'Best First'.
Re: CGI delete attributes set in textfield()
by Fletch (Bishop) on Feb 21, 2008 at 14:51 UTC

    You've got a static argument list, so there's no way to "delete" them short of editing the code. So instead of hard-coding it what you need to do: build up the argument list in a variable, adding in the onfoo attributes if needed, then pass that dynamic list to the textfield method.

    my @textfield_args = ( -name => 'name', ..., -maxlength => 120, ); if( $was_no_change ) { push @textfield_args, -onfocus => 'this.value = ''"; push @textfield_args, -onblur => 'this.value = 'def'"; push @textfield_args, -onchange => 'delete onfocus, delete onblur"; } $string .= $cgi->textfield( @textfield_args );

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Thank you Fletch, but I know of a change through the -onchange method of javascript available in CGI so it is not quite that what you coded for me. It would be much easier (IMHO) if there would be a possibility that do the following
      -onchange => "override onfocus, override onblur",
      Nevertheless thank you for your help.

        Aaah I misunderstood what you were asking, which is a Javascript question and not really a Perl problem (whatever JS library you're using should provide some method of removing or changing event bindings; e.g. jquery would be something like $("textfield[name=name]").unbind( 'focus' ); $("textfield[name=name]").unbind('blur');).

        Update: tweaks to sample jquery and link'd to unbind docs

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Re: CGI delete attributes set in textfield()
by tilly (Archbishop) on Feb 21, 2008 at 18:11 UTC
    Untested, but this might work without having to pull any libraries in:
    $string .= $cgi->textfield( -name => 'name', -default => 'def', -override => 0, -size => 20, -maxlength => 120, -onfocus => "if (null == this.changed) this.value = ''", -onblur => "if (null == this.changed) this.value = 'def'", -onchange => "this.changed = true", );