Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make clone of an element in jQuery?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 252
    Comment on it

    Hello friends,

    Today we learn how to make clone of an element using jQuery. In jQuery, clone() method is used to make clone or copy of a set of element or a particular element. This method makes not only the copy of element but also it's child, attributes and content also. In other words, we can say that this method create the duplicate element of a particular element.

    Syntax:

    $(selector).clone(true|false)

    selector is the element whose clone we want to creator. true | false means whether to make the copy of the event handler or not. By default value is false

    Let's take and example to understand this method.

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("button").click(function(){
            $("body").append($("p:first").clone(true));
            $("div").clone().appendTo("body");  ;
        });
    });
    </script>
    </head>
    <body>
    
    <div>
      <p>first para</p>
      <ul>
        <li>list1</li>
        <li>list2</li>
      </ul>
    </div>
    
    <button>Create clone of the element</button>
    
    </body>
    </html>
    

    In above example, when you click on the button one time it will make the copy of p and the div element. If you will again click it will again make the copy and so on.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: