Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Jquery difference between .append() and .appendTo() method

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 784
    Comment on it

    Hello Readers,


    If we want to insert any element or content within define particular element using jQuery, then we can use jQuery .append() or .appendTo() method.


    jQuery .append() and .appendTo() both methods are doing the similar task, add a html content or element after the content of the matched elements.


    Example:

    <div class="block">I'm a big block</div>
    <div class="block">I'm a big block 2</div>
    


    Here we can see the major difference in the following syntax.


    Syntax 1 for .append() method: $(selector).append(new text);


    $('.block').append("<div class="newbox">I'm new block by prepend</div>"); 
    


    Syntax 2 for .appendTo() method: $(new text).appendTo(selector);

    $("<div class="newbox">I'm new block by appendTo</div>").appendTo('.block');
    


    The result will be for the both methods:


    Jquery .append and .appendTo() Both methods above are doing the similar task, but with different syntax, the new contents after .append() or .appendTo() will become:

    <div class="block">
       I'm a big block
       <div class="newbox">I'm new block by prepend</div>
    </div>
    <div class="block">
       I'm a big block 2
       <div class="newbox">I'm new block by prepend</div>
    </div>
    


    Thanks


 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: