Home › Forums › JavaScript › Regular plugin init (how it works?)
- This topic is empty.
-
AuthorPosts
-
July 9, 2014 at 7:41 am #175054KuzyoParticipant
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?
July 9, 2014 at 8:01 am #175055Paulie_DMemberIt’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.
July 9, 2014 at 9:03 am #175056KuzyoParticipantI 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?
July 9, 2014 at 9:07 am #175057nixnerdParticipantAre you just trying to write a function??
July 9, 2014 at 9:46 am #175058SenffParticipantThis may help explain how it works: http://learn.jquery.com/plugins/basic-plugin-creation/
July 9, 2014 at 9:51 am #175060nixnerdParticipantHey @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.
July 9, 2014 at 11:47 am #175068KuzyoParticipantThanks @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.
July 9, 2014 at 12:14 pm #175070SenffParticipantIn 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.
July 9, 2014 at 12:18 pm #175071nixnerdParticipantBut… 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.
July 9, 2014 at 12:31 pm #175072SenffParticipantOh, 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.
July 9, 2014 at 6:55 pm #175109nixnerdParticipantBut to try and figure out how the fuel injection exactly works…that’s a whole different story.
True dat…
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.