0

My journey being at least on above basic level in front end is still on and I stumbled upon quite a big problem recently.

I can select DOM element, like

var element=document.getElementById("elementid")

and then add some function to it, like this

function testFunction() {
    alert(this.getAttribute('data-self'));
}
element.customFunction=testFunction;

But is there by chance any way of doing this using jQuery?

Tried with attr(), prop(), data() and without any luck on that matter. data() was a close one though, because it allows me to execute function using $('#my-element-id').data('customFunction')(); for example, but still doesn't solve my problem, as this new property of that selected buttons is not accessible any other way.

To sum up: What is the simplest way to add generic function (like in an example) to collection of DOM elements in a way that it's accessible like any other property?

ilmash
  • 174
  • 2
  • 12

2 Answers2

2

Adding a function directly to a DOM element is not considered a good practice for a variety of reasons.

I'd suggest a jQuery plugin method which is pretty easy:

jQuery.fn.myMethod = function() {
    // iterate all items in the jQuery collection and apply any logic you want
    return this.each(function() {
        // `this` will be the DOM element so you can carry out your operation here
        // for example, to flip a background color
        if (this.tagName === "INPUT") {
            this.style.backgroundColor = "red";
        } else {
            this.style.backgroundColor = "blue";
        } 
    });
}

// usage of jQuery plugin method:
$("#elementid").myMethod();
$(".boxes, .ovals, .containers").myMethod();

You can also pass arguments to your jQuery plugin method and use those arguments in the implementation of your custom method.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I guess this will somehow solve my problem. I'll wrap some handler as in this example and then call appropriate functions inside it, depending on data passed. I know it's not good practice to add directly to DOM, but it will be internal solution for very specific task. I already have written simple webserver and aggreed on data interface, now all I have left is frontend, which appears to me as hell in comparison to backend ;-) – ilmash Feb 02 '16 at 22:22
  • Even more, I can abuse your method and do `$(selector).each(function() { this.customFunction=testFunction; });` which works, compared to my other tries and fails in that matter. I will keep in mind that it's bad practice and I promise not to spread it ;-) – ilmash Feb 02 '16 at 22:35
0

It's pretty similar. jQuery just returns an object so you can add functions to it.

var myElement = $('#elementid');
function testFunction() {
    alert($(this).attr('data-self'));
}
myElement.customFunction=testFunction;

myElement.customFunction();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="elementid" data-self="some value"></div>
Rupert
  • 1,629
  • 11
  • 23
  • This, sir, is basically the same thing I posted. I know I can select element this way, but it still requires some looping around to get desired effect. Hard to switch from static typed languages to JS, my my... – ilmash Feb 02 '16 at 22:20
  • Pretty sure this answers the question: "what is the simplest way to add generic function" and "is there by chance any way of doing this using jQuery" – Rupert Feb 02 '16 at 22:27