HTML::Object::DOM::NodeList - HTML Object DOM NodeList Class
use HTML::Object::DOM::NodeList; my $list = HTML::Object::DOM::NodeList->new || die( HTML::Object::DOM::NodeList->error, "\n" );
v0.2.0
NodeList objects are collections of nodes, usually returned by properties such as "childNodes" in HTML::Object::DOM::Node and methods such as "querySelectorAll" in HTML::Object::DOM::Document.
NodeList
The number of nodes in the NodeList.
Example:
# All the paragraphs in the document my $items = $doc->getElementsByTagName("p"); # For each item in the list, # append the entire element as a string of HTML my $gross = ""; for( my $i = 0; $i < $items->length; $i++ ) { $gross += $items->[$i]->innerHTML; } # $gross is now all the HTML for the paragraphs
See also Mozilla documentation
Inherits methods from its parent Module::Generic::Array
Executes a provided code reference (reference to a subroutine or anonymous subroutine) once per NodeList element, passing the element as an argument to the subroutine.
my $node = $doc->createElement("div"); my $kid1 = $doc->createElement("p"); my $kid2 = $doc->createTextNode("hey"); my $kid3 = $doc->createElement("span"); $node->appendChild( $kid1 ); $node->appendChild( $kid2 ); $node->appendChild( $kid3 ); my $list = $node->childNodes; $list->forEach(sub(currentValue, currentIndex, listObj) { my( $currentValue, $currentIndex, $listObj ) = @_; say( "$currentValue, $currentIndex, $_" ); });
Returns an item in the list by its index, or undef if the index is out-of-bounds. An alternative to accessing nodeList[i] (which instead returns undefined when i is out-of-bounds). This is mostly useful for non-JavaScript DOM implementations.
undef
my $tables = $doc->getElementsByTagName( 'table' ); my $firstTable = $tables->item(1); # or $tables->[1] - returns the second table in the DOM
Returns an iterator, allowing code to go through all the keys of the key/value pairs contained in the collection. (In this case, the keys are numbers starting from 0.)
my $node = $doc->createElement("div"); my $kid1 = $doc->createElement("p"); my $kid2 = $doc->createTextNode("hey"); my $kid3 = $doc->createElement("span"); $node->appendChild( $kid1 ); $node->appendChild( $kid2 ); $node->appendChild( $kid3 ); my $list = $node->childNodes; # Using for..of foreach my $key ( $list->keys->list ) { say( $key ); }
Returns an iterator allowing code to go through all values (nodes) of the key/value pairs contained in the collection.
my $node = $doc->createElement("div"); my $kid1 = $doc->createElement("p"); my $kid2 = $doc->createTextNode("hey"); my $kid3 = $doc->createElement("span"); $node->appendChild( $kid1 ); $node->appendChild( $kid2 ); $node->appendChild( $kid3 ); my $list = $node->childNodes; # Using for..of foreach my $value ( $list->values ) { say( $value ); }
Jacques Deguest <jack@deguest.jp>
Mozilla documentation
Copyright(c) 2022 DEGUEST Pte. Ltd.
All rights reserved
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install HTML::Object, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTML::Object
CPAN shell
perl -MCPAN -e shell install HTML::Object
For more information on module installation, please visit the detailed CPAN module installation guide.