1
<!-- html -->
<h1>Title 1</h1>
<p>paragraph 1</p>
<p>paragraph 2</p>
<p>paragraph 3</p>
<h1>Title 2</h1>
<p>paragraph 1</p>
<p>paragraph 2</p>
<p>paragraph 3</p>

<!--jQuery -->
$('#numPara').bind('click', function(){
    var i=0;
    var bouton = $(this);
    $('p').nextUntil('h1').each(function(index, element){
        console.log(element);
        $(element).prepend((index+1) + '.' + (index+1) + " ");
        bouton.attr('disabled', false);
        });

    });
}); 

Question: How can I number the paragraphs from the number of the title? I already 'bind' my button to number the paragraphs but it doesn't apply on the first paragraph of each section and it doesn't follow the number of the title. Teacher told me that I should put limits but I've been searching for a few hours now and I have no idea how to do it. Thank you for any help.

Example : 1. Title1
          1.1 paragraph1
          1.2 paragraph2
          1.3 paragraph3
          2. Title2
          2.1 paragraph1
          2.2 paragraph2
          2.3 paragraph3
falsarella
  • 12,217
  • 9
  • 69
  • 115

1 Answers1

1

Given that html, place this code accordingly when you need to run the script:

$('h1').each(function(hIndex, element){
    hIndex++;
    var next = $(this).next();
    var pIndex = 0;
    while (next.is('p')) {
        pIndex++;
        next.prepend(hIndex + '.' + pIndex + ' ');
        next = next.next();
    }
});

http://jsfiddle.net/z28KQ/

falsarella
  • 12,217
  • 9
  • 69
  • 115
  • 1
    Thank you very much...it's the "var next = $(this).next();" that I was missing...very appreciated...I understand now – user2359135 May 10 '13 at 16:50
  • @user2359135 Glad I helped you! BTW, if that solved your problem, choose this answer as the correct one. – falsarella May 10 '13 at 19:10