Hunter
Hunter

Reputation: 1601

Need to wrap specified content

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 ?

Upvotes: 1

Views: 43

Answers (4)

Sampson
Sampson

Reputation: 268512

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/

Upvotes: 1

Jashwant
Jashwant

Reputation: 29025

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(' ')); 

Upvotes: 1

xdazz
xdazz

Reputation: 160983

var div = $('.left');
div.html(div.html().replace(/(\d+)/g, '<span>$1</span>'));

Upvotes: 2

Sudip
Sudip

Reputation: 2051

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.

Upvotes: 1

Related Questions