Chartjs-Updating chartjs pie chart by using .keypress() not working

0👍

The problem was fixed by switching the listener from a very specific set of elements to the document with the previous selector as selector of the on method. Complete discussion enclosed:

The label element will not emit a keypress event as far as I know. Are you sure you don’t mean .kkTable #dataTable_filter > label input ? – Douwe de Haan

Yeah i did. Just tried changing it to that, still doesn’t work however :/ – LENide300_KK

Does it log the 1? Because if not, there must be some other problem. Can you also append your HTML to the question? – Douwe de Haan

No it does not, which also confuses me. I would do that, but the thing is I pull the data from a table that gets generated through PHP and then some sorting + search functions gets applied to the table using datatables.net. So I can’t quite post the HTML without it being ridiculously long. – LENide300_KK

When you set the listener (the keypress part), is the HTML already there? Or are you generating the HTML at a later moment? Because that would explain why it doesn’t work. Try changing your function to the following: $(document).on('keypress', '.kkTable #dataTable_filter > label input', function() {Douwe de Haan

The HTML is already there. However, your suggestion worked. But I dont understand why? I thought $(document).on('keypress', '.kkTable #dataTable_filter > label input', function() {‘ is the same as $('.kkTable #dataTable_filter > label').keypress(function() { ? Do you have an idea about why that is? – LENide300_KK

The difference is that all events bubble up (propagate) the DOM tree and eventually end up on the document. The listener I provided listens to keypress events on the document and afterwards checks wether or not the event came from something that is according to the filter. So even with HTML that changes after the listener is set it would still work, the one you had only works with elements that both checks with the filter and are present at the moment it is called. – Douwe de Haan

Leave a comment