Auto onclick in function

Asked

Viewed 72 times

0

Personal explaining a little how the system works.

When the user enters a value below the established goal, it automatically opens a modal to fill in an "action plan".

By completing the plan, he can create other actions or conclude, but if he leaves it open, every time he enters that place, another modal will appear stating that there is an open plan.

If the user closes the system and comes back to analyze this plan, the right serious: It appears that "Modal Info" he click the button and would take it to the plan page.

Err: When I came back I don’t know what the hell I did, Onclick went automatic. As soon as it enters the area, the system detects that it has an open plan, and already opens its page. Follow the code of the function below.

function showModals(indicadorID, name, city, month, year, role){
    checkPlan(name, city, year, function (result) {
        //Exemplo de operacao
        //mes a definir em indicadorForaDaMeta
        checkIndicadorMeta(name, city, month, year, role, function (indicadorForaDaMeta) {
            var mesAtual = new Date().getMonth();
            if (result.length == 0) {
                var mesDoUltimoPlano = mesAtual - 1;
            } else {
                var mesDoUltimoPlano = new Date(result[result.length - 1].pla_data).getMonth();
            }
            console.log(result);
            if (indicadorForaDaMeta && !(mesAtual == mesDoUltimoPlano)) {
                $('#add-modal-plano-de-acao-form').find('[name="id"]').val(indicadorID);
                $('#add-modal-plano-de-acao').modal('show');
            } else {
                for (let i = 0; i < result.length; i++) {
                    if (result[i].pla_status) {
                        var month = new Date(result[i].pla_data).getMonth() + 1;
                        var year = new Date(result[i].pla_data).getFullYear();
                        $('#info-modal-plano-de-acao-form').find('[name="id"]').val(result[i].pla_id);
                        $('#info-modal-plano-de-acao-form').find('[name="month"]').val(month);
                        $('#info-modal-plano-de-acao-form').find('[name="year"]').val(year);
                        $('#info-modal-plano-de-acao').modal('show');
                        console.log("#" + convertNameToId(name) + "-botao-plano");
                        $('#' + convertNameToId(name) + '-botao-plano').removeAttr('onClick');
                        $('#' + convertNameToId(name) + '-botao-plano').attr('onClick', abrirPlano(result[i].pla_id, month, year));
                       
                        i = result.length; 
                    }
                }
            }
        });

    });
}

  • .attr('onClick' is not the correct way to assign code to the click event. In jquery, which seems to be what you are using, you should do .on('click', function() {....}, or in the old version .click(function(){ ... }. In the sequence of this the .removeAttr('onClick') would be .unbind('click')

  • @Isac thank you. I made the changes you suggested and it worked, it’s no longer automatic, but... The Madal button is working, but another button is not. Follow the code. //code <button class="btn-Warning btn-lg" id="example-flat-button">Action Plan</button>

1 answer

0

This is because on this line:

$('#' + convertNameToId(name) + '-botao-plano').attr('onClick', abrirPlano(result[i].pla_id, month, year));

you are expressly saying that the attribute "onclick" has the value of the function output.

try switching to use a callback:

$('#' + convertNameToId(name) + '-botao-plano').attr('onClick', function() { abrirPlano(result[i].pla_id, month, year) });

so the attribute of "onclick" gets the value of the anonymity function that when called will call the function abrirPlano(....)

  • Moshmage, Thanks! I made the changes you suggested, but it’s still automatic.

  • 1

    @Strange Letimberg. It shouldn’t. That’s the only code that puts the value of the onclick attribute to that element ? There’s no other code that does that?!

  • yes. The solution that friend Isac mentioned, worked partially.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.