Forgive me for being stupid, but are these two effectively the same?
The only reason I ask is I've seen the former in a plugin I'm adapting, and I was a little thrown by the fact it wasn't simply using prependTo() instead;
$('li:last', list)
.clone()
.insertBefore( $('li:first', list) );
Following on from my initial question, and concerning @Nick's comments regarding cloning, would these two scenarios produce the same outcome?
// 1
for (i = 1; i <= number; i++) {
$('li:first', list)
.clone()
.insertAfter( $('li:last', list) );
$('li:first', list).remove();
}
// 2
for (i = 1; i <= number; i++) {
$('li:first', list).appendTo(list);
}
It depends on the usage.
insertBefore() literally inserts an element before an element whereas prependTo() inserts an element at the beginning (but within) an element.
In your case, $('li:last', list).clone().insertBefore( $('li:first', list) ); will insert the last element before the first li element.
If you were to use $('li:last', list).clone().prependTo( $('li:first', list) );, the last li element would be added inside of the first li element, which you dont want. To use prependTo() in the same manner as insertBefore, you would need to do
$('li:last', list).clone().prependTo( $(list) ); (assuming list is a reference to the UL tag.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With