0

So quick example

//example
var el = document.getElementById("test");
el.classList.add("my-class");

//my function
var myFunction = function(){//code goes here};
el.myFunction();

//and can we take it further by adding a method to it like classList does
el.myFunction().myMethod();

I know I'm probably not using the right lingo here, because a method is a function of a class which means myFunction() must be a class. I'm just confused on if something like this is possible. Anyways...

How can i create a function that can be chained to an element like that?

Thanks for your help

John_911
  • 1,124
  • 2
  • 21
  • 38
  • https://www.sitepoint.com/javascript-like-boss-understanding-fluent-apis/ – bishop Jun 01 '16 at 15:10
  • this line `el.myFunction();` will already throw an exception – mic4ael Jun 01 '16 at 15:12
  • If you're looking for the right lingo, search for "How do I add methods to DOM elements". You'd have to add the methods `myFunction` and `myMethod` to the class of `el`. – Bergi Jun 01 '16 at 15:12
  • For chaining, see [this](https://stackoverflow.com/search?q=method+chaining+[js]). Notice that a better approach would be `myFunction(el).myMethod().myMethod()` – Bergi Jun 01 '16 at 15:14
  • I know libraries that do what you want, but instead I'd recommend looking in functional programming and function currying. – mugabits Jun 01 '16 at 15:15
  • Thanks to everyone. I didn't know the lingo to search for this stuff. I'll check it out – John_911 Jun 01 '16 at 15:20

0 Answers0