jquery - wrap each image in a div and prepend a number -


i'm trying write function wrap each image in div , insert span label each image.

this have written far:

$('#carousel img').each(function(index) {   $(this).wrap('<div class="image"></div>');   $(this).before(function (index) {     index = '00' + (index + 1);     return '<span class=index>' + index.substr(index.length - 2) + '</span>';   }); }); 

what code doing this, notice number problem , putting span inside img tag:

<div class="image">   <img src="img/ny1.jpg" />     <span class="index">01</span>      </img> </div>  <div class="image">   <img src="img/ny2.jpg" />     <span class="index">01</span>      </img> </div> 

the desired result of this:

from this:

<img src="img/ny1.jpg" /> <img src="img/ny2.jpg" />

to this:

<div class="image"> <span class="index">01</span>    <img src="img/ny1.jpg" /> </div>  <div class="image"> <span class="index">02</span>    <img src="img/ny1.jpg" /> </div> 

thanks

if problem you're having numbering (because see nothing else wrong), remove index $(this).before(function (index) {

jsfiddle example


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -