Honestly, the problem will be solved after we get all the <h2> tags following each <h1> and immediately before the next <h1> , right?
This code did it and works great
$("h1").each(function(mainHeadIndex) { // Numbering the main heads $(this).html(mainHeadIndex +1 +'. ' + $(this).html()); // Find all the h2 tags right under this particular main head $(this).nextUntil("h1").filter("h2").each(function(subIndex) { // calculate the right sub head number var newSubIndex = subIndex+1; // Numbering the sub heads $(this).html(mainHeadIndex +1 +'.'+ newSubIndex +$(this).html()); }); })
you can check it by this link
PS: thank you MatejB, I did not know jsfiddle.com before your message :)
Mohammed ElSayed
source share