Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: how to I append $(this)

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.

like image 674
Zak Henry Avatar asked Nov 22 '10 03:11

Zak Henry


People also ask

How do you append something in jQuery?

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.

What does $( this mean in jQuery?

$(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.

What is the difference between the append () and appendTo () methods?

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.

How append a div in another div using jQuery?

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.


1 Answers

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()); 
like image 179
David Tang Avatar answered Oct 08 '22 08:10

David Tang