Nizam Ali
Nizam Ali

Reputation: 241

Wrap the text inside a <div> using JQuery

I'm working on replacing a particular text with a wrapping that particular text. Anyhow I'm confused in doing so. I'm able to find the particular words matching my scenario. But, I need to replace them inside a

Here is my html

<small>
    Size: M<div class="item-devider"></div> 
    checkbox_test: Yes<div class="item-devider"></div>
    I don't mind about color: No<div class="item-devider"></div> 
    test: No<div class="item-devider"></div> 
    test_group: select1<div class="item-devider"></div>
</small>

JQuery

$('.item-devider').map(function(){
    var text = this.previousSibling.nodeValue.indexOf(': No') == -1 ?  undefined  : this.previousSibling.nodeValue;              
    if(text!= undefined) {

    }
});    

Fiddle http://jsfiddle.net/EwNRJ/1135/

Any ideas would be greatly appreciated

Upvotes: 0

Views: 65

Answers (3)

Felix
Felix

Reputation: 38112

You can do:

$('#sample small').contents().filter(function() {
    return this.nodeType === 3 && $.trim(this.nodeValue).indexOf(': No') > -1;
}).wrap('<div class="highlight" />');

Updated Fiddle

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

Try,

var texts = $('.item-devider').map(function(){
    return this.previousSibling.nodeValue.indexOf(': No') > -1;        
});

This above code will return the div which is having a string in its previous position which contains the text ': No'

And also try this too,

var texts = $('#sample small').contents().filter(function(){
    return $(this).text().indexOf(': No') > -1;     
}).wrap('<div/>');

DEMO

Upvotes: 1

xdazz
xdazz

Reputation: 160923

Return undefined in the callback of .map will remove the elements not needed.

var texts = $('.item-devider').map(function(){
    return this.previousSibling.nodeValue.indexOf(': No') == -1 ?  undefined  : this.previousSibling.nodeValue;
}).get();

The working demo.

Upvotes: 0

Related Questions