Youss
Youss

Reputation: 4212

jQuery find and append element

I created a variable to find 'img' and clone like this:

var element = jQuery.find('img').clone();
element.appendTo('body');

It doesn't work for me. My Chrome console tells me:

Uncaught TypeError: Object [object Array] has no method 'clone'

Is it even possible to write jQuery.find() like that? What else can I try?

Upvotes: 2

Views: 15009

Answers (3)

charlietfl
charlietfl

Reputation: 171669

find() requires a starting point which you haven't provided. Syntax of jQuery.find() is incorrect. Proper syntax for find() is :

jQuery(parentSelector).find(descendentSelector);

This will search within the parentSelector for the descendentSelector.

Upvotes: 3

udidu
udidu

Reputation: 8588

The clone() function not working well on the find() results, you can wrap the find() result with jQuery object like so:

var element = jQuery(jQuery.find('img')).clone();
element.appendTo('body');

Upvotes: 1

dansaania
dansaania

Reputation: 163

You could just use

$('img').appendTo('body');

or

$('img').clone().appendTo('body');

Upvotes: 4

Related Questions