839 posts
  • Power Elite Author
  • Winner
  • Top Monthly Author
  • Trendsetter
+10 more
wpbakery
says

Hey,

How can I select “text” in the following code and wrap it in a “span” tag?

<div class="wrap">
   This text should be selected and wrapped in a span
   <span class="span_class_name">Another content</span>
</div>

Thanks guys!

1799 posts
  • Affiliate Level 1
  • Author Level 4
  • Beta Tester
  • Collector Level 2
+4 more
BroOf
says
2627 posts Put a Donk On It
  • Weekly Top Seller
  • Elite Author
  • Exclusive Author
  • Featured Author
+11 more
ThemeProvince
says

Use the .text() function.


var textsample = $(' div.wrap ').text();

textsample = "<span>" + textsample + "</span>";

// Clear contents of wrap div then add the text again.

$(' div.wrap ').html("").append(textsample);

I think this will work.

1383 posts
  • 5 Years of Membership
  • Affiliate Level 2
  • Collector Level 2
  • Exclusive Author
+1 more
wpCanyonThemes
says

I think he’s not gonna have that exact sentence every time :)

@mixey – This was a tough one, thank you for this little challenge :) Here you go:

$('.wrap').each(function(){

    $(this.childNodes).filter(function(index) {
        if(this.nodeType === 3 && this.nodeValue != ''){ return true; }
    }).wrap('<span>');

});
</span>
Edit: Stupid forum eating my code :) Gimme a sec.

Here you go http://pastie.org/1579886

839 posts
  • Power Elite Author
  • Winner
  • Top Monthly Author
  • Trendsetter
+10 more
wpbakery
says

Thanks to everyone and especially to Boba.

That did the trick!

1383 posts
  • 5 Years of Membership
  • Affiliate Level 2
  • Collector Level 2
  • Exclusive Author
+1 more
wpCanyonThemes
says

Thanks to everyone and especially to Boba. That did the trick!

You’re welcome :)

4483 posts ThemeForest Senior Reviewer
  • Affiliate Level 1
  • Author Level 6
  • Collector Level 3
  • Envato Team
+14 more
Ivor
Envato team
says

This is why I effin’ love ThemeForest. :)

by
by
by
by
by
by