1

This is my code, this message coming from dynamicaly

<div class="left">6 item(s) - Page 1 of 3 </div>

Need to change

<div class="left"> <span>6</span> item(s) - Page <span>1</span> of <span>3</span> </div>

Any idea ?

Hunter
  • 1,515
  • 4
  • 15
  • 25
  • see this: [link](http://stackoverflow.com/questions/10896332/jquery-wrap-multiple-div-elements-on-same-class/10897813#10897813) – Alex Ball Jun 06 '12 at 06:12

4 Answers4

2
var div = $('.left');
div.html(div.html().replace(/(\d+)/g, '<span>$1</span>'));
xdazz
  • 158,678
  • 38
  • 247
  • 274
1

Split the text by "Space". You will get the array of items. Take the index, I thought It will always same and add your "span" on that.

Sudip Pal
  • 2,041
  • 1
  • 13
  • 16
1

This works for me. I simply check if a word is a number. If yes, I wrap it inside a span

var left = $('.left');
var html = left.html().split(' ');
for(var i=0,len=html.length;i<len;i++){ 
    if(!isNaN(html[i]) && html[i]!=''){
        html[i] = '<span>' + html[i] + '</span>';
    }
}
left.html(html.join(' ')); 
Jashwant
  • 28,410
  • 16
  • 70
  • 105
1

Use the implicit looping that comes natively in jQuery to reset the html of each matched element.

​$(".left").html(function(i,v){
    return v.replace( /(\d+)/g, "<span>$1</span>" );
});​​

Fiddle: http://jsfiddle.net/jonathansampson/gEYEE/

Sampson
  • 265,109
  • 74
  • 539
  • 565