I have problem to get current index size when I add a div with jQuery
HTML:
<div>
<p>1</p>
</div>
<button>Add</button>
jQuery:
$("button").live("click",function() {
var index = $("div").index()+1;
$("div").append('<p>'+index+'</p>');
});
Playground: http://jsfiddle.net/uZwWg/
Must I use the each function or is there another solution?
fiddle
You should point to the children element to get the desired index
and use
+2(index is zero based) as you’re already starting with1