Intermittent problem with Uncaught Typeerror: Cannot read Property 'locac' of Undefined

Asked

Viewed 319 times

0

I’m having this problem

Uncaught Typeerror: Cannot read Property 'locac' of Undefined

With the code:

function fnc(){    
    var evt = (window.event ? window.event : event);
    var elemento = evt.target;
    var params = $(elemento).data('params');
    alert(params.locac);
}

$('body').on('click','button.inst',fnc);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" data-params='{"locac":"reg","titulo":"Inserir Registro"}' class="btn btn-success inst">Inserir</button>

Example link: example

But not always the error occurs, there are times I click on the button several times and does not give this error, more times I click once and already gives error.

Sometimes there’s a mistake and there’s no mistake.

The only mistake I see in real world

  • It’s always working right here for me, both in the snipper here in the OS, and in jsFiddle. (I’m testing in Chrome) for you it doesn’t work?

  • I am in Chrome as well, within jsFiddle I realized that there is no error at any time for some reason inside the original code of the above mentioned error.. @Fernando

  • If the error is not reproducible it is complicated that someone can help you because it can be anything in your environment, and that is not accessible to us through the question. Even so, I will try to help, the first thing that comes to mind is jQuery’s version, which you are using?

  • @Fernando Jquery2.1.3, and in this example I have an error because Firefox does not recognize Global Vent, I had to go through parameters, but this part this blz.

1 answer

1

There are no apparent errors in your code, but I believe the solution is to place your script inside the ready jQuery. Read here.

Do so:

$(function () {
    function fnc(){    
        var evt = (window.event ? window.event : event);
        var elemento = evt.target;
        var params = $(elemento).data('params');
        alert(params.locac);
    }

    $('body').on('click','button.inst',fnc);
});

It’s good practice to always use this pattern when you need to manipulate the DOM.

  • The HTML structure is loaded via ajax, and this button comes bundled with this HTML.

  • I will edit the question as soon as possible.

Browser other questions tagged

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