I have a jQuery method that I want to append the element $(this) to.
My code is
$("#actions ul" ).append($(this));
However this does seem to do anything. Using the line
.append($(this).html())
does work, but it only grabs the child of $(this) (a div) not the whole element (an li).
I'm probably doing something moronic, hopefully someone can point this out to me.
jQuery append() MethodThe append() method inserts specified content at the end of the selected elements. Tip: To insert content at the beginning of the selected elements, use the prepend() method.
$(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this . Calling jQuery a second time (which is a mistake) on the result of $(this) returns an new jQuery object based on the same selector as the first one.
With . append() , the selector expression preceding the method is the container into which the content is inserted. With . appendTo() , on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted into the target container.
First, select the div element which need to be copy into another div element. Select the target element where div element is copied. Use the append() method to copy the element as its child.
Without knowing exactly what this
refers to, the only reason I can think of for $(this).html()
to work and not $(this)
is that the former method creates a new element from the html code whereas appending $(this)
will move the element.
See if the below fixes your problem:
$("#actions ul" ).append($(this).clone());
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