Home > Mobile >  how to add space in the input after focus?
how to add space in the input after focus?

Time:10-12

The purpose is to add space after executing focus on an input which have an id = "editor".

let editor = document.getElementById("editor") as HTMLElement;
editor.focus();
// here I want to add   on the focused HTMLElement "body"

I try to dispatch a KeyboardEvent to add space or any letter but this didn't worked.

editor.dispatchEvent(new KeyboardEvent('keypress',{'key':'a'}))

CodePudding user response:

You can try below code:

editor.addEventListener('focus', function(event){
   if(!event.target.value){
       editor.value = ' '
   }
})

CodePudding user response:

If it's angular then just this


<input type="text" (focus)="$any($event.target).value  = ' '">
<input type="text" (focus)="$any($event.target).value = $any($event.target).value   ' '">

Should work. (Use the second one if the first one fails)

  • Related