Tuesday, 9 February 2021

get element just in front or after the cursor

const elementEditing = document.getElementById('example');
elementEditing.addEventListener("keydown", onkeydownInEditable);

function onkeydownInEditable(e, KeyboardEvent) {
  if (e.key === "Enter") {
    e.preventDefault();
  }
  if (e.key === "Backspace") {
  
    console.log('is el[notContentDeletable] just in front cursor and will be deleted ?')
  
  } else if ( e.key === "Delete") {
        
    console.log('is el[notContentDeletable] just behind cursor and will be deleted ?')
    
  }
}
<div id="example" contentEditable="true">
aaaaa 1<input notContentDeletable>2 bbbbb
</div>

I want to prevent that an element with [notContentDeletable] attribute gets deleted by "del" or "backspace".

(in jsfiddle)

  • if im between 1 and input and press del the input will be deleted. I want to prevent this.

  • if im between input and 2 and press backspace the input will be deleted. I want to prevent this.

So how can I get the element next to or after the cursor? Is that possible?



from get element just in front or after the cursor

No comments:

Post a Comment