jQuery adding object as list item

12 views
Skip to first unread message

Paul Menard

unread,
Feb 20, 2012, 1:58:14 PM2/20/12
to refresh...@googlegroups.com
I'm having a brain malfunction or something. Need a second set of eyes for some jQuery code.

The basic process is that I'm selecting a section of the site and grabbing all the anchors with a specific class. No big deal there. Then for each object found I need to append this as a list item to another section (sidebar) <ul></ul>

I need to be agnostic about the anchor since it sometime will contain text and sometimes an image and sometime both. So I'm trying to just grab the anchor object which will contain the anchor and all children.

Here is my simple code. The destination element '#sidebar-users-others ul' is already present but empty.


jQuery('div#content-area a.users').each(function() {

// Clone the found anchor...
var user_a_html = jQuery(this).clone();

// This works but it adds the anchor inside the <ul> But I need this wrapped in <li></li>
//jQuery('#sidebar-users-others ul').append(avatar_a_html);

// This does not works.
jQuery('#sidebar-users-others ul').append('<li>+avatar_a_html+'</li>');


});

Jean Vestal

unread,
Feb 20, 2012, 2:03:46 PM2/20/12
to refresh...@googlegroups.com
Use jQuery wrap to wrap each anchor in #sidebar-users-other in <li></li>.

Jean

--
Our Web site: http://www.RefreshAustin.org/

You received this message because you are subscribed to the Google Groups "Refresh Austin" group.

[ Posting ]
To post to this group, send email to Refresh...@googlegroups.com
Job-related postings should follow http://tr.im/refreshaustinjobspolicy
We do not accept job posts from recruiters.

[ Unsubscribe ]
To unsubscribe from this group, send email to Refresh-Austi...@googlegroups.com

[ More Info ]
For more options, visit this group at http://groups.google.com/group/Refresh-Austin



--
Jean M. Vestal
512.983.5032
jeanm...@gmail.com


Paul Menard

unread,
Feb 20, 2012, 2:07:31 PM2/20/12
to refresh...@googlegroups.com
Never mind. I found a solution. Append the empty list item first then in a second append the object like

jQuery('#sidebar-users-others ul').append('<li></li>');
jQuery('#sidebar-users-others ul li:last').append(avatar_a_html);

Unless someone has a more elegant solution.

P-

Paul Menard

unread,
Feb 20, 2012, 2:08:31 PM2/20/12
to refresh...@googlegroups.com
Jean,

Thanks. Yeah forgot about wrap();

Travis Swicegood

unread,
Feb 20, 2012, 2:16:21 PM2/20/12
to Refresh Austin
Another options is to simply create a <li> object, appending the <a>
object to that.

Here's a jsfiddle version that shows what I'm talking about.
http://jsfiddle.net/LmcUE/4/

-T
Reply all
Reply to author
Forward
0 new messages