How to prepend a consecutive sequence number to every paragraph using jQuery? -
in ui, have button , paragraphs. requirement prepend consecutive sequence numbers every paragraph on clicking of button.
i tried way. did not work.
var count=0; $("#buttongparano").click(function(){ jquery.each($("#col2 p").not(".append").prepend(++count)); }); });
on 1st click, prepending '1' every paragraph, on 2nd click, prepending '2' , on. want when click once, 1st para should have sequence no. 1, 2nd paragraph should have sequence no. 2 , on.
the paragraphs written this:
<div id="col2"> <p class="firstparagraph"> lorem ipsum sit amet, consecte elit, sed diam nonummy nibh euismod <em>tincidunt</em> ut laoreet dolore magna aliquam erat </p> <p class="secondparagraph"> duis autem vel eum iriure dolor in hendrerit in vulputate velit esse <strong>molestie</strong> consequat, vel illum eu feugiat nulla facilisis @ vero eros et accumsan et iusto odio</p> </div>
my code should first find how many paragraphs there totally, , assign them consecutive value. mean code should work number of paragraphs. please suggest if has idea.
no need store own counter, use .each()
iterate elements , use built-in index:
runnable snippet below:
$("#buttongparano").click(function(){ $("#col2 p").each(function(index) { $(this).prepend(index + 1); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="button" id="buttongparano" value="click" /> <div id="col2"> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> </div>
Comments
Post a Comment