How to append a space into <p> 'activeElement' ?


#1

Hello & Thanks,
Sorry , I can’t get the three ticks working .
(windows pc) Is it three quote marks or what ?
I have this paragraph :

 <p contenteditable="" id="p_tgh">Text Paragraph</p>
window.addEventListener("keydown", onPress_ENTER, false);
// Which I don't allow the 'enterkey' to be pressed

function onPress_ENTER(event){
    var keyPressed = event.keyCode || event.which;
    //if ENTER is pressed
    if(keyPressed==13)
    {
    // Good here: 
        event.preventDefault();
        stopImmediatePropagation();
    // Do stuff here too:
    }
    else
    {
        return false;
    }
}

Which works fine .
Since that

has focus I now want to insert a space ,
or actually 4 spaces " " .
Pls, how can this be done ?
Thanks


#2

The paragraph:
<p>contenteditable="" id="p_tgh" Text Paragraph </p>
Ah there it is ‘backwards tick’ :sleeping:
Thanks


#3

I edited your post to add the right formatting. :madscientist:


#4

Where do you want the 4 spaces to be added?

  • At the beginning?
  • At the end?
  • At the current position of the caret?

#5

At the end .
Thanks


#6

I know I can do this sort of thing:

<script>
var  someDots = "....";
var origContents = "";
var newContents = "";
function myFunction() {

  origContents = document.getElementById("demo").innerHTML ; 
  newContents = origContents + someDots; 
  document.getElementById("demo").innerHTML = newContents; 
}
</script>

But how do I determine which element was clicked ,
because there will be many paragraphs and the number of paragraphs changes dynamically ; and each paragraph won’t have an “id=” .
Thanks