I want to create a text input which does not allowed to input any character (same as disabled input, but the mouse cursor still shown)
function loadPage() { const el = document.getElementById('input-id'); el.addEventListener('keydown', (event) => { event.preventDefault(); }); } loadPage(); <input maxlength="0" id="input-id"> The above code works fine for normal alphabet characters. However, when I use IME to type Japanese fullwidth character, it does not work (still can input character)
Do you know where is the problem, and is there anyway to workaround?
EventObject.preventDefault()belongs to theEventObject, not the Element. TheEventObjectis passed to the EventListener when anEventoccurs, likeel.onkeyup = function(eventObj){ eventObj.preventDefault(); }. My guess is you really wantreadonly='readonly'as an HTML attribute, in this case.keydowndoesn't cut it, you probably need to add the listener for other related events too. (input? keyup? keypress? Try it, one of those might do it for you)