Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is the difference between $('div') and $('
    ') in jquery ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 100
    Comment on it

    Hello Readers,

    $('div') : In jquery this element selects all the div element present on the page.
    $('< div />') : In jquery this element create the new div element and this div element is not added to DOM tree unless you dont't append it to any DOM element.
    For Example :
    $('< div />').addClass('sample');
    $('div').addClass('sample');
    $('< div/>').addClass('sample') : this line of code create a new div element and add class sample to it. But the new created div is not in the DOM tree at that time, you may need to append it to some other element.
    $('div').addClass('sample') : this line of code add class sample to all the div elements in the DOM tree.

    So, in above the $('< div/>') element only new element gets created but it is not added to DOM. One have to append it to any other element use the below jquery code.

    $('<div/>').addClass('classname').appendTo('body');
    

    In the above code, new created div add to body.

 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: