• Post Reply Bookmark Topic Watch Topic
  • New Topic

selecting pure text in jquery

 
jeroen dijkmeijer
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a nested list and I would like to put span tags around the text of the headers of sublists.


this should become:



I've tried a few things already:

But this puts a span around the text and the UL tag.


this gives an error (text()) is not a function



doesn't work at all No error no output.

I'm a bit mystified hopefully somebody here can help me.
kind regards,
Jeroen.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65664
129
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Text nodes are special in the DOM -- the wrap functions can only work with element nodes. I'd recommend fetching the text contents, creating a new span with those contents, and then replacing the old contents with the new span element.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd also ask *why* you want to put a span around them--might it be better to add a class to the list element of the header items?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to do this with JavaScript. Can;t you do this on the serverside to start out with?

Eric
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!