File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Engineering
»
HTML, CSS and JavaScript
Author
selecting pure text in jquery
jeroen dijkmeijer
Ranch Hand
Joined: Sep 26, 2003
Posts: 131
posted
Jul 28, 2010 10:17:07
0
Hello,
I have a nested list and I would like to put span tags around the text of the headers of sublists.
<ul><li>head 1<ul> <li>item1.1</li> <li>head1.2<ul> <li>item1.2.1</li> </ul></li> </ul></li></ul>
this should become:
<ul><li><span>head 1</span><ul> <li>item1.1</li> <li>head1.2<ul> <li><span>item1.2.1</span></li> </ul></li> </ul></li></ul>
I've tried a few things already:
$('li', this).wrapInner('<span></span>');
But this puts a span around the text and the UL tag.
$('li', this).wrapInner(new function () { return '<span class='+ this.text() + '></span>';});
this gives an error (text()) is not a function
$('li', this).text().wrapInner('<span></span>');
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
Joined: Jan 10, 2002
Posts: 56209
13
I like...
posted
Jul 28, 2010 10:37:53
0
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.
[
Smart Questions
] [
JSP FAQ
] [
Books by Bear
] [
Bear's FrontMan
] [
About Bear
]
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12617
I like...
posted
Jul 28, 2010 12:18:29
0
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
Joined: Nov 08, 2001
Posts: 15357
6
posted
Jul 28, 2010 13:33:19
0
Why do you need to do this with JavaScript. Can;t you do this on the serverside to start out with?
Eric
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: selecting pure text in jquery
Similar Threads
List item disappears in Firefox
jQuery -- add remove list item
CSS not working in Firefox, works in IE
Menu image positioning
javascript dropdown over text field
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter