How to create a function that

I'm not a programmer, I'm just a web designer with a bit of programming skills, and I ask you to help me learn a little more about jquery / javascript.

I have one function that calls 2 different divs (test1 and test2)

myFunction ($("#test1"));
myFunction ($("#test2"));

I want to try to achieve something similar to

myFunction ($("#test1") $("#test2"));

Because I want to try to use one call to call multiple divs. Also, my problem is that my ipotetical myFunction should look like this:

myFunction (param1, param2){ if(typeof param2 == "undefined"){param2 = $(window)}; // do something}

I have no idea how to create a multiple function call to perform the same action, but for different elements, and with the fact that I can have several parameters, this does not make it easier for me.

Any suggestion on how to do this? or some resource on which I could read? thanks in advance!:)

+3
2
myFunction($("#test1, #test2, #test3"));

http://api.jquery.com/multiple-selector/

+3

: plugin:

<div>Hello</div>
<div class="blah">Another</div>
<div class="foo">And another</div>


$.fn.myFunc = function() {

    // loop over all matches
    this.each(function() {

        // do something with each match
        alert($(this).text());            
    });
    return this;
};

// call it on any set of elements like this
$("div").myFunc();

: http://jsfiddle.net/sbcvg/

+3

Source: https://habr.com/ru/post/1784726/


All Articles