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

I've been looking through the CGI documentation (briefly) and CGI Programming with Perl (much more thoroughly) and I appear to have reached an impasse in my research. I am trying to determine if CGI.pm can add an id="blah" to the HTML it outputs, however, I have either not found it or missed it in the CGI documentation.

Can CGI.pm generate the id attribute in HTML or am I out of luck?

Replies are listed 'Best First'.
Re: Quick CGI Question
by Zaxo (Archbishop) on Aug 27, 2002 at 18:38 UTC
    perl -MCGI=:standard -e'print p({-id=>"foo"},"Bar")' <p id="foo">Bar</p>

    You can even make up new attributes, and CGI.pm will try to do the right thing.

    After Compline,
    Zaxo

Re: Quick CGI Question
by the pusher robot (Monk) on Aug 27, 2002 at 18:26 UTC
    Use a hash ref as the first arg. Any keys will be used as attributes:
    div({id=>'div1'}, "text"); <div id="div1">text</div> a({href=>'http://perlmonks.org', id=>'pmlink'}, "Perlmonks.org"); <a href="http://perlmonks.org" id="pmlink">Perlmonks.org</a>
Re: Quick CGI Question
by peschkaj (Pilgrim) on Aug 27, 2002 at 18:18 UTC
    Ah, so it cannot be appended to elements? E.G.
    <table id="inputTable"> or <div id="myDiv">

      from perldoc:CGI:

      If the first argument is an associative array reference, then the keys and values of the associative array become the HTML tag's attributes:
      so doing: table({-id=>"blah", .... gives you what you want on any element.
      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/
Re: Quick CGI Question
by fglock (Vicar) on Aug 27, 2002 at 18:12 UTC

    (I removed my answer)

    update: sorry, I didnīt understand the question. I thought "id" was something like a form "input" field name.

    See below for correct answers.