W3Schools.com

Document anchors Collection

Document Object Reference Document Object

Definition and Usage

The anchors collection returns an array of all the anchors in the current document.

Syntax

document.anchors[].property


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The anchors collection is supported in all major browsers.


Examples

Example 1

Return the number of anchors in the document:

<html>
<body>

<a name="html">HTML Tutorial</a><br />
<a name="css">CSS Tutorial</a><br />
<a name="xml">XML Tutorial</a><br />

<p>Number of anchors:
<script type="text/javascript">
document.write(document.anchors.length);
</script></p>

</body>
</html>

The output of the code above will be:

Number of anchors: 3

Try it yourself »

Example 2

Return the innerHTML of the first anchor in the document:

<html>
<body>

<a name="html">HTML Tutorial</a><br />
<a name="css">CSS Tutorial</a><br />
<a name="xml">XML Tutorial</a><br />

<p>innerHTML of first anchor:
<script type="text/javascript">
document.write(document.anchors[0].innerHTML);
</script></p>

</body>
</html>

The output of the code above will be:

InnerHTML of first anchor: HTML Tutorial

Try it yourself »


Document Object Reference Document Object
WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$7.95/mo SEO Hosting
Premium Website Design
WEB BUILDING
XML Editor - Free Trial!
FREE Website BUILDER
Free Website Templates Free CSS Templates
Make Your Own Website
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE