Home > Blockchain >  Adding characters to the end of a line
Adding characters to the end of a line

Time:02-20

Based on the following code and description here:

Add Bullets to Each New Line within a textarea

const bullet = "\u002a";
const bulletWithSpace = `${bullet} `;
const enter = 13;


const handleInput = (event) => {
    const { keyCode, target } = event;
    const { selectionStart, value } = target;

    if (keyCode === enter) {
        //console.log('debug');
        target.value = [...value]
            .map((c, i) => i === selectionStart - 1
                ? `\n${bulletWithSpace}`
                : c
            )
            .join('');
        //console.log(target.value);

        target.selectionStart = selectionStart bulletWithSpace.length;
        target.selectionEnd = selectionStart bulletWithSpace.length;
    }

    if (value[0] !== bullet) {
        target.value = `${bulletWithSpace}${value}`;
    }
}

How can this be modified to work in the opposite way?

Instead of adding the target to the first position of each new line. Change the target position to the end.

Note: I replaced the bullet unicode to * for testing

Original code does this:

* text
* new line

Changing the code to do instead:

text *  
new line *  

* = bullet in my example code

CodePudding user response:

I Changed your code and you can see the working example at https://nexuscode.online/editor/R0iIh4wDkO

  • Related