How to work with document.anchors in JavaScript?

HTMLJavascriptProgramming Scripts

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


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

Live Demo

<!DOCTYPE html>
      <title>JavaScript Example</title>
         <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>
         var num1 = document.links.length;
         var num2 = document.anchors.length;

         document.write("How many links? "+num1);
         document.write("<br>How many anchors? "+num2);
Updated on 23-Jun-2020 08:59:38