You must Sign In to post a response.
  • Category: JQuery

    Difference between $('selector').methodname() and $.methodname() while calling method

    Hi,

    Generally, in jquery we use syntax like $('selector').methodname() to call any method. But sometimes I have seen jquery scripts where they use $.methodname directly without any selector in brackets following dollar sign i.e. $('selector').methodname()

    Please state the difference between $('selector').methodname() vs $.methodname() while calling method.
  • #768971
    Hi,

    I think this ('selector') option is mostly used for the CSS property related things rather than $.methodname() will be more of helper function.

    Suppose consider a CSS function,
    like color = green
    so if we use this selector we can specify like

    $('selector').css('color','green')


    Where as $.methodName() act like a helper class or function and will not return the wrapper data. i.e $.ajax ()
    But $.('selector') will return the wrapper data to jquery methods like .hide() , .show()

    Thanks,
    Mani

  • #768990
    Hai Jayesh,
    The major difference between the $('selector').methodname() vs $.methodname() is that when we need to method chaining, we use $('selector').methodname() where we can attach the other method with the selector while the $.methodName() is just a helper method which will provide the help for JQuery healper methods like $.Ajax() and this will not return any collection of the data.
    It is just to perform something. Another example is like $.trim() where we can provide the value to trim.
    The $('selector').methodname() will return the data for the selector. For example $( "div" ).something() will do something and will return the div element.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • Sign In to post your comments