-3

Please take a look at the following code. I am going through it and wondering what does .clone() do here in the context of my code.

The jQuery documentation says that "The .clone() method performs a deep copy of the set of matched elements, meaning that it copies the matched elements as well as all of their descendant elements and text nodes."

var x = 0;

functionPick()
{               
    var $loading = $('<img src="../images/loading-small.gif" wth="16" height="16">...Preparing');

    var y = '';

    y = '?ID=' + encodeURIComponent(<cfoutput>#ID#</cfoutput>);


    if(x != 0)
    {

        x.remove();
        x = 0;

    }

    x = $('<div></div>').append($loading.clone());


Please explain.
Jack
  • 989
  • 3
  • 13
  • 24

2 Answers2

1

It does nothing useful in that code. It makes a copy of the elements (and nodes) in the jQuery set, but in your case, there's no reason to, as they'll be created afresh each time the function is called.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Your code is incomplete dont exactly know what you doing with x, but for your help.

Clone()

Create a deep copy of the set of matched elements.

so if you:

<div class="container">
<div class="hello">Hello</div>
<div class="goodbye">
Goodbye
<div class="hello">Hello</div>
</div>
</div>

and running this code:

$( ".hello" ).clone().appendTo( ".goodbye" );

result would be:

<div class="container">
<div class="goodbye">
Goodbye
<div class="hello">Hello</div>
</div>
</div>
Community
  • 1
  • 1
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110