December 2, 2000 - Extended Tag Extraction
December 2, 2000 Extended Tag Extraction Tips: December 2000
Yehuda Shiran, Ph.D.
|
getElementsByTagName()
method, which extracts an array of elements of the same tag type. This method applies to any element. You can extract all tags in a whole document, or in any other elements such a DIV
, or a P
. The following handleAllTags()
function extracts all FONT
tags in this tip as well as all tags in the DIV
container that wraps the following code listing. It prints these two counts:
<SCRIPT LANGUAGE="JavaScript">
<!--
function handleAllTags() {
var arrayOfDocFonts, arrayOfDivFonts;
if (document.all || document.getElementById) {
arrayOfDivFonts = foo.getElementsByTagName("font");
arrayOfDocFonts = document.getElementsByTagName("font");
}
else {
document.write("Unrecognized Browser Detected");
}
alert("Number of font tags in document and DIV are " + arrayOfDocFonts.length + " and " +
arrayOfDivFonts.length + ", respectively.");
}
// -->
</SCRIPT>
DIV
with ID="foo"
are found by foo.getElementsByTagName("font")
. All this tip's tags are extracted by document.getElementsByTagName("font")
. This tip has been contributed by Thomas Ashe.