I want to convert .bind
of jQuery to JavaScript. Here is the code:
$("#cke_bdy").bind("keypress", function(e) { some code... });
Can anybody please help?
Advertisement
Answer
Related to the Mozilla documentation of javascript :
Add an event listener on any element should be coded as :
element.addEventListener(type, listener, useCapture);
Remove an event listener on any element should be coded as :
element.removeEventListener(type, listener, useCapture);
Example :
// For instance var onClickOnId = function () { console.log('click on #id'); } document.getElementById('#id').addEventListener('click', onClickOnId, true); document.getElementById('#id').removeEventListener('click', onClickOnId, true);
But a question persist : Why would you not use jQuery to bind your events ?
By the way, .bind() is deprecated and should be replaced by .on() in the latest releases.