I have an IFrame within my page and am using the designMode="on" on this iframe for a small-level rich text editing.
My concern is that I need not allow the user to enter new lines in it - meaning, I want to restrict enter keys.
I did it using the question posted here to listen to the keypress events. But in my keypress event, if I return false, nothing happens.
How do I restrict enter keys in Iframe with designmode?
Code :
document.getElementById('editor').contentWindow.addEventListener('keypress',restrictEnterKey, true);
function restrictEnterKey(event) {
var key=(event.charCode)?event.charCode:((event.keyCode)?event.keyCode:((event.which)?event.which:0));
if (key == 13) {
//alert('me');
return false;
}
return true;
}
You need to call preventDefault
on the event or set returnValue
to false
for IE. The following function will do the job:
function preventDefault(evt) {
if (evt.preventDefault) {
evt.preventDefault();
} else if (typeof evt.returnValue !== "undefined") {
evt.returnValue = false;
}
}