Reputation: 241
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
<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>
$('.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
Reputation: 38112
You can do:
$('#sample small').contents().filter(function() {
return this.nodeType === 3 && $.trim(this.nodeValue).indexOf(': No') > -1;
}).wrap('<div class="highlight" />');
Upvotes: 0
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/>');
Upvotes: 1
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();
Upvotes: 0