wyc
wyc

Reputation: 55343

Wrapping text with that isn't wrapped with any HTML tag (jQuery)?

I have some text in the following div: .wpbdp-listing-single The text has nothing wrapping it. How to trap an HTML tag around it? (without wrapping the other elements that do have html tags?

Upvotes: 1

Views: 383

Answers (1)

Dan Dascalescu
Dan Dascalescu

Reputation: 151893

So you want to only wrap text-only elements of that div. Here's jQuery code that does that:

$('.wpbdp-listing-single').contents().filter(function() {
  return this.nodeType == 3;
})​.wrap('<b></b>');​​​​​​​​​​

Unlike .children(), .contents() will return text nodes as well, and you can filter for text nodes only by testing for the nodeType property.

Fiddle: http://jsfiddle.net/dandv/YkgLa/

Upvotes: 2

Related Questions