How to show the number of links in a document with JavaScript?

JavascriptWeb DevelopmentFront End Scripts

To get the number of links in JavaScript, use the links property with length property.

Example

You can try to run the following code to display the number of links in a document −

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <a href="https://www.tutorialspoint.com/php">PHP</a>
         <br>
      <a href="https://www.tutorialspoint.com/java/">Java</a>
      <br>
      <a href="https://www.tutorialspoint.com/html5/">HTML</a>
      <br>
      <a href="https://www.tutorialspoint.com/css/">CSS</a>
      <script>
         var val = document.links.length;
         document.write("<br>Number of links in the document: "+val);
      </script>
   </body>
</html>
raja
Published on 26-Feb-2018 12:28:52
Advertisements