0

I have an app that corrects the text.

window.addEventListener("keydown", function(e) {
            if (e.code == "Space") {                
                format();
            }
        }, true);

The format function is one that parses the value of textarea, highlight errors and returns the HTML of the formatted area.

function format() {
      data = {
        paragraphs: 0,
        sentences: 0,
        words: 0,
        hardSentences: 0,
        veryHardSentences: 0,
        adverbs: 0,
        passiveVoice: 0,
        complex: 0
      };

      ("use strict");     

      var text = document.querySelector('div[contenteditable][aria-label="Message Body"]').innerText;
      console.log("this is text " + text)
      let paragraphs = text.split("\n");
      let hardSentences = paragraphs.map(p => getDifficultSentences(p));
      let inP = hardSentences.map(para => `<div>${para}</div>`);
      data.paragraphs = paragraphs.length;
      OutputText = inP.join("");
      return (composeView.setBodyHTML(OutputText))
      }

I want to invoke the format() function each time the users writes a new word, hence I use to spacebar to implement that behavior.

What happens is that after I invoke the format function, the cursors go at the beginning of the textarea.

What's the best way to preserve the cursor position always at the end of the textarea (without jQuery)?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
leonardofed
  • 936
  • 2
  • 9
  • 24

0 Answers0