NAME
HTML::Object::DOM::NodeList - HTML Object DOM NodeList Class
SYNOPSIS
my
$list
= HTML::Object::DOM::NodeList->new ||
die
( HTML::Object::DOM::NodeList->error,
"\n"
);
VERSION
v0.2.0
DESCRIPTION
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.
PROPERTIES
length
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
METHODS
Inherits methods from its parent Module::Generic::Array
forEach
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.
Example:
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, $_"
);
});
See also Mozilla documentation
item
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.
Example:
my
$tables
=
$doc
->getElementsByTagName(
'table'
);
my
$firstTable
=
$tables
->item(1);
# or $tables->[1] - returns the second table in the DOM
See also Mozilla documentation
keys
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.)
Example:
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
);
}
See also Mozilla documentation
values
Returns an iterator allowing code to go through all values (nodes) of the key/value pairs contained in the collection.
Example:
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
);
}
See also Mozilla documentation
AUTHOR
Jacques Deguest <jack@deguest.jp>
SEE ALSO
COPYRIGHT & LICENSE
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.