JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 6th, 2012, 07:43 PM
aeternus's Avatar
aeternus aeternus is offline
For POny!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: Amsterdam
Posts: 416 aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 4 h 56 m 43 sec
Reputation Power: 114
Add before closing tag

Hi guys,

I was fiddling a bit with jquery and for some reason i can't get to insert a <span> right before the endtag of the parent element.

To make it more clear I have a li and idealy i want to clone() that li and add to that clone a span.

Code:
<ul>
  <li>
    <input type="submit" name="bla[]" />
  </li>
<!-- second li ++  should have spans -->
  <li>
    <input type="submit" name="bla[]" />
     <span>crap</span>
  </li>
</ul>


What I was thinking of is to set a variable with the contents of li and than append the span to that content. But in I rather do that in the process of cloning. But I am missing something.

This is what works, but only for the inner content, not for the cloning.

javascript Code:
Original - javascript Code
  1.  
  2. // some script
  3.                 var test = $('li:first').html();
  4.                 $('li').html(test+'<span>crap</span>');
  5.  
  6. // i rather put this inside this script i made
  7. $("li:first").clone(true).appendTo('ul');


Edit1: I played a bit more and I have something that works, but it seems a bit redundant anyone know a more optimal way?
javascript Code:
Original - javascript Code
  1.  
  2. //make a copy
  3. var copied = $("li:first").clone(true);
  4. // get the innerhtml
  5. var content = $(copied).html();
  6. // add some crap to the list
  7. $(copied).html(content+'<span>crap</span>');
  8. // append it
  9. $(copied).appendTo('ul');
  10.  
__________________
PHP Tutorial

Last edited by aeternus : December 6th, 2012 at 08:02 PM.

Reply With Quote
  #2  
Old December 7th, 2012, 10:49 AM
jonathansampson jonathansampson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Atlanta, GA
Posts: 14 jonathansampson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 22 m 47 sec
Reputation Power: 0
You could go with the following:

javascript Code:
Original - javascript Code
  1. // Start with a cached reference to our first list item(s)
  2. var original = $("li:first-child");
  3.  
  4. // Now clone them (and their events), append a new <span> and insert
  5. original.clone(true).append("<span>Foo</span>").insertAfter(original);
Comments on this post
aeternus agrees: cheers! I overlooked append, which unlike its name adds stuff inside

Reply With Quote
  #3  
Old December 7th, 2012, 10:52 AM
aeternus's Avatar
aeternus aeternus is offline
For POny!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: Amsterdam
Posts: 416 aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 4 h 56 m 43 sec
Reputation Power: 114
ah the sneaky append(). For some reason I overlooked that one because I thought it posts the content outside of the selector.
Cheers! Although I wont use the last part otherwise it keeps adding it right after the first row (list-item), even if 3 rows(list-items) are added.

thanks mate

Reply With Quote
  #4  
Old December 7th, 2012, 10:54 AM
jonathansampson jonathansampson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Atlanta, GA
Posts: 14 jonathansampson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 22 m 47 sec
Reputation Power: 0
If you have more than one item, you would .appendTo the parent of the cloned item, definitely. I was just going with your code example, which only had one list item. Glad I could help.

Reply With Quote
  #5  
Old December 7th, 2012, 10:55 AM
aeternus's Avatar
aeternus aeternus is offline
For POny!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: Amsterdam
Posts: 416 aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 4 h 56 m 43 sec
Reputation Power: 114
Quote:
Originally Posted by jonathansampson
If you have more than one item, you would .appendTo the parent of the cloned item, definitely. I was just going with your code example, which only had one list item. Glad I could help.

Cheers mate, I gave you a digital reputation coin to celebrate
edit: hmm it's not showing up it seems, ah well you get the gesture

Last edited by aeternus : December 7th, 2012 at 10:58 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Add before closing tag

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap