Computer >> Computer tutorials >  >> Programming >> Javascript

How to work with document.anchors in JavaScript?


Use the document.anchor property in JavaScript to get the number of anchors (<a>) in a document with the name attribute.

Example

You can try to run the following code to implement document.anchors property in JavaScript −

<!DOCTYPE html>
<html>
   <head>
      <title>JavaScript Example</title>
   </head>
   <body>
      <p>
         <a href="/java/index.htm">Java</a><br>
         <a href="/php/index.htm">PHP</a><br>
         <a name="Ruby Tutorial">Ruby</a><br>
         <a name="WordPress Tutorial">WordPress</a>
      </p>
      <script>
         var num1 = document.links.length;
         var num2 = document.anchors.length;

         document.write("How many links? "+num1);
         document.write("<br>How many anchors? "+num2);
      </script>
   </body>
</html>