0

I'm adding an event listener to my paragraph element in Javascript. My paragraphs are inside my 'main' HTML element. when the user presses the keyboard button 'AltGr' (key code 18) the paragraph is supposed to become a child of its predecessor. But the event itself isn't firing. Why is this and how do I solve the problem?

let m=document.getElementById('textEdit');

function pKeyClick(e){
if (e.keyCode=="18"){ //AltGr
    e.preventDefault();
    let parent=m.childNodes[m.childNodes.length-2];
    let li=document.createElement("li");
    m.replaceChild(li,e.target);
    parent.appendChild(ul);
    console.log(parent.childNodes)
    e.stopPropagation();
  }
}

The event gets attached whenever p is generated.

jamie
  • 176
  • 1
  • 12
  • 1
    Can you use the rich snippet editor to create a reproducible example? – imjared Nov 29 '19 at 16:45
  • [possible duplicate](https://stackoverflow.com/questions/3149362/capture-key-press-or-keydown-event-on-div-element) – pavan kumar Nov 29 '19 at 16:48
  • I don't anything in this code that would actually cause these functions to even kick in. Why did you chose to show this code in particular? For the purposes of your bug, almost all this code is irrelevant: show how `init()` gets called, so that it's clear it even runs, remove any code that deals with `m`(it's not important for testing the problem you have right now) and remove all the code inside `pKeyClick`, replacing it with the single line `console.log(e, e.key, e.keyCode)`. If your problem really is that the event doesn't fire, that's <10 lines of code that you can make a runnable snippet – Mike 'Pomax' Kamermans Nov 29 '19 at 16:53
  • I think alt gr is a dead key. It doesn't fire events, it modifies keys that come after it. See https://en.m.wikipedia.org/wiki/Dead_key and https://en.m.wikipedia.org/wiki/AltGr_key – Ruan Mendes Nov 29 '19 at 17:04
  • what can an alternate key code be? – jamie Nov 29 '19 at 17:05
  • I find tab to be annoying to use – jamie Nov 29 '19 at 17:08

0 Answers0