How to create or reset counters with JavaScript?


To create counters, use the counterReset property in JavaScript. You can try to run the following code to create or reset one or more counters with JavaScript −

Example

Live Demo

<!DOCTYPE html>
<html>
   <head>
      <style>
         h2:before {
            counter-increment: section;
            content: counter(section) " Quiz ";
         }
      </style>
   </head>
   <body>
      <h1>Quizzes</h1>
      <p>Change the counterIncrement</p>
      <h2>Ruby</h2>
      <h2 id="myID">Java</h2>
      <h2>PHP</h2>
      <h2>Perl</h2>
      <button onclick="display()">Reset Counter</button>
      <script>
         function display() {
            document.body.style.counterReset = "section";
         }
      </script>
   </body>
</html>

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

520 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements