Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Regular plugin init (how it works?)

  • This topic is empty.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #175054
    Kuzyo
    Participant

    Hi guys.

    Every plugin that I used I init in this way(for example):

    $('element').datepicker();

    Can somebody explain, how it woks? what should I write to implement this feature by myself?

    #175055
    Paulie_D
    Member

    It’s not clear what you are asking for but that looks like the standard way that Jquery selects an element (or group of elements) and applies a predefined function.

    http://courses.tutsplus.com/courses/30-days-to-learn-jquery

    #175056
    Kuzyo
    Participant

    I know that for jQuery plugins, you shoud simply add your plugin to$.fn. object:

    jQuery.fn.myPlugin = function(options) {
      // Do your awesome plugin stuff here
    };
    

    How to implement it in vanills JS?

    #175057
    nixnerd
    Participant

    Are you just trying to write a function??

    #175058
    Senff
    Participant

    This may help explain how it works: http://learn.jquery.com/plugins/basic-plugin-creation/

    #175060
    nixnerd
    Participant

    Hey @Senff, I think he’s just trying to write a function because he wants to know how to use “vanilla JS” for whatever he’s trying to do.

    I could TOTALLY be misunderstanding, as I don’t really get what’s going on here.

    #175068
    Kuzyo
    Participant

    Thanks @Senff. To have ability init plugin as usual in jQuery I need simply write:

    $.fn.greenify = function() {
        this.css( "color", "green" );
    };
    
    $( "a" ).greenify(); // Makes all the links green.
    

    How can I do it in raw JS? Thanks.

    #175070
    Senff
    Participant

    In this (very simple) case, it would probably be something like:

        var elems = document.getElementsByTagName('a');
        for (var i = 0; i<elems.length; i++) {
                elems[i].style.color = 'green';
            }
        }
    

    Or something. Untested.

    I’m not sure why you’re asking. There’s a whole library written for you to make it this kind of stuff easy. To go back to the nitty gritty of it (and do what jQuery is doing for you) will take quite a lot of work…unnecessary work, if you ask me, cause I don’t think you’ll learn all too much from it.

    Don’t get me wrong, but it seems you are trying to understand the exact inner workings of a car, when you want to learn how to drive (and not be a car mechanic), if you know what I mean.

    #175071
    nixnerd
    Participant

    But… there are certain circumstances where having an external library really isn’t beneficial. Don’t get me wrong, I have no idea what OP is even trying to do with this little app here. But… jQuery is not the solution to every problem.

    This is more or less a continuation of another thread OP started a few days ago.

    #175072
    Senff
    Participant

    Oh, I wasn’t implying that. I only meant it in this specific case. :)

    If you want to learn how to be a driver, it’s OK to want to know some basics of how a car works.

    But to try and figure out how the fuel injection exactly works…that’s a whole different story.

    #175109
    nixnerd
    Participant

    But to try and figure out how the fuel injection exactly works…that’s a whole different story.

    True dat…

Viewing 11 posts - 1 through 11 (of 11 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.