tags:

views:

171

answers:

2

Get/Set innerHTML in Perl HTML::TreeBuilder? I could get innerHTML but dont know how to set.

Thanks in Advance.

+1  A: 

I'm not sure if this approach will satisfy you, but you can use html($html) method from pQuery:

This method is akin to the famous JavaScript/DOM function innerHTML.

If called with no arguments, this will return the the inner HTML string of the first DOM element in the pQuery object.

If called with an HTML string argument, this will set the inner HTML of all the DOM elements in the pQuery object.

As far as why pQuery may satisfy you, to quote from POD:

pQuery::DOM is roughly an attempt to duplicate JavaScript's DOM in Perl. It subclasses HTML::TreeBuilder/HTML::Element so there are a few differences to be aware of. See the pQuery::DOM documentation for details.

DVK
+1  A: 

I'd use pQuery, but this will work

#!/usr/bin/perl --
use strict;
use warnings;
use HTML::TreeBuilder;

my $html = <<'__HTML__';
<div id="target">old <B>i</B><I>n</I>ner</div>
__HTML__

{
    my $t = HTML::TreeBuilder->new_from_content($html);

    print $t->as_HTML('<>&',' ',{}), "\n";

    my $target = $t->look_down( id => 'target' );
    $target->delete_content;
    $target->push_content(
        HTML::TreeBuilder->new_from_content(
            "<B>NEW</B>"
        )->look_down(qw!_tag body!)->detach_content
    );

    print $t->as_HTML('<>&',' ',{}), "\n";

}
__END__
<html>
 <head>
 </head>
 <body>
  <div id="target">old <b>i</b><i>n</i>ner</div>
 </body>
</html>

<html>
 <head>
 </head>
 <body>
  <div id="target"><b>NEW</b></div>
 </body>
</html>

Yes, I RTFM

junk
Works perfect, it even links back _parent. Thanks
iavian