How to make a jQuery function respond to two distinct events

0

The jQuery function I created makes asynchronous requests as follows:

var meuObjeto = (function () {

    return {
        minhaFuncaoAjax: function (url, linkContent) {
            $.ajax({
                url: url,
                type: 'GET',
                data: { data: linkContent},
                success: function ( response ) {
                    console.log('Deu certo! ' + JSON.stringify(response));
                }
            });
        }
    };

})();

Now, I have a select with several URLs, in the .change () event it sends a URL to my function.

$("#meu-select").on("change", function() {
    let meuLink = $(this).val();
    meuObjeto.minhaFuncaoAjax(meuLink, linkContent);
});

However, I have several IDs on separate links in the pages, when I click on the links they send their contents in the .click () event for the linkContent to be handled in the URL passed by the select.

As the links are generated dynamically, I should use event delegation:

$(document).on("click", ".meus-links", function () {
    let linkContent = $(this).attr("data-id");
    meuObjeto.minhaFuncaoAjax(url, linkContent);
});

How can I keep the function correctly updated if the parameters for it are sent by different events?

On the one hand, the link is sent by the select and, on the other, the ID is sent by the clicked link.

How do I get the function to get both values correct?

I'm on the bus, writing on my cell phone. I apologize for any error.

    
asked by anonymous 30.10.2018 / 00:17

0 answers