Discuss Add before closing tag in the JavaScript Development forum on Dev Shed. Add before closing tag JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
Posts: 416
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
// some script
var test = $('li:first').html();
$('li').html(test+'<span>crap</span>');
// i rather put this inside this script i made
$("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?
Posts: 416
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.
Posts: 14
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.
Posts: 416
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.