Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery onclick registration error

I have a button that I want to attach a click listener to, but everytime the code runs, it throws a console error

jquery.js:4435 Uncaught TypeError: ((n.event.special[g.origType] || (intermediate value)).handle || g.handler).apply is not a function

Here's the js code that triggers the error, the first line runs fine, the second line is the one that causes the error

$toolbar.off('click', '.btn-save'); // $toolbar is assigned $("#toolbar") on init           
$toolbar.on('click', '.btn-save', function(e){
    saveData(0);
});

What confuses me is, if I run that bit of code manually through the console, I get no errors

Here's the HTML

<div class="row" id="toolbar">
<div class="col-lg-12">
    <button type="button" class="btn btn-primary btn-save">Save</button>
    <button type="button" id="btnCancel" class="btn btn-default btn-cancel">Cancel</button>
</div>
</div>
like image 349
iceman2992 Avatar asked Mar 24 '16 16:03

iceman2992


1 Answers

I have found the problem, below my listeners, there's an unrelated listener that I attached

$(document).on("click", "#btn-back", Views.Editor.close);

The problem here is that Views.Editor.close is the wrong method name. When I changed the method name, the error disappears

I didn't realize that an unrelated listener can affect others

like image 104
iceman2992 Avatar answered Sep 30 '22 01:09

iceman2992