Preserve cursor position in javascript

Multi tool use
Multi tool use












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)?










share|improve this question

























  • Can you show us your format() function?

    – Mirakurun
    Jan 2 at 12:30











  • @Mirakurun addded

    – leonardofed
    Jan 2 at 12:31











  • @Mirakurun majority of the work is done by getDifficultSentences() that call lots of other internal utilities.

    – leonardofed
    Jan 2 at 12:33






  • 1





    Try this. stackoverflow.com/questions/4233265/…

    – AKT
    Jan 2 at 13:06






  • 1





    Also, I think you should use composeView.getBodyElement() and composeView.getTextContent(). There might be multiple compose windows open.

    – AKT
    Jan 2 at 13:10
















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)?










share|improve this question

























  • Can you show us your format() function?

    – Mirakurun
    Jan 2 at 12:30











  • @Mirakurun addded

    – leonardofed
    Jan 2 at 12:31











  • @Mirakurun majority of the work is done by getDifficultSentences() that call lots of other internal utilities.

    – leonardofed
    Jan 2 at 12:33






  • 1





    Try this. stackoverflow.com/questions/4233265/…

    – AKT
    Jan 2 at 13:06






  • 1





    Also, I think you should use composeView.getBodyElement() and composeView.getTextContent(). There might be multiple compose windows open.

    – AKT
    Jan 2 at 13:10














0












0








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)?










share|improve this question
















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)?







javascript keydown cursors






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 12:31







leonardofed

















asked Jan 2 at 12:26









leonardofedleonardofed

342114




342114













  • Can you show us your format() function?

    – Mirakurun
    Jan 2 at 12:30











  • @Mirakurun addded

    – leonardofed
    Jan 2 at 12:31











  • @Mirakurun majority of the work is done by getDifficultSentences() that call lots of other internal utilities.

    – leonardofed
    Jan 2 at 12:33






  • 1





    Try this. stackoverflow.com/questions/4233265/…

    – AKT
    Jan 2 at 13:06






  • 1





    Also, I think you should use composeView.getBodyElement() and composeView.getTextContent(). There might be multiple compose windows open.

    – AKT
    Jan 2 at 13:10



















  • Can you show us your format() function?

    – Mirakurun
    Jan 2 at 12:30











  • @Mirakurun addded

    – leonardofed
    Jan 2 at 12:31











  • @Mirakurun majority of the work is done by getDifficultSentences() that call lots of other internal utilities.

    – leonardofed
    Jan 2 at 12:33






  • 1





    Try this. stackoverflow.com/questions/4233265/…

    – AKT
    Jan 2 at 13:06






  • 1





    Also, I think you should use composeView.getBodyElement() and composeView.getTextContent(). There might be multiple compose windows open.

    – AKT
    Jan 2 at 13:10

















Can you show us your format() function?

– Mirakurun
Jan 2 at 12:30





Can you show us your format() function?

– Mirakurun
Jan 2 at 12:30













@Mirakurun addded

– leonardofed
Jan 2 at 12:31





@Mirakurun addded

– leonardofed
Jan 2 at 12:31













@Mirakurun majority of the work is done by getDifficultSentences() that call lots of other internal utilities.

– leonardofed
Jan 2 at 12:33





@Mirakurun majority of the work is done by getDifficultSentences() that call lots of other internal utilities.

– leonardofed
Jan 2 at 12:33




1




1





Try this. stackoverflow.com/questions/4233265/…

– AKT
Jan 2 at 13:06





Try this. stackoverflow.com/questions/4233265/…

– AKT
Jan 2 at 13:06




1




1





Also, I think you should use composeView.getBodyElement() and composeView.getTextContent(). There might be multiple compose windows open.

– AKT
Jan 2 at 13:10





Also, I think you should use composeView.getBodyElement() and composeView.getTextContent(). There might be multiple compose windows open.

– AKT
Jan 2 at 13:10












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006382%2fpreserve-cursor-position-in-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006382%2fpreserve-cursor-position-in-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







wiLckaPUHj nP7hJU gLuNERToLj6Ed,t8
H0PINm3rAmi Gt2e5AcXLzWTTr 6cqcs5 UXEz2C94 sB RlHvmlLGLCkYg4vR2Cqw2,s09lthFDl voYc NyKQfYv6HyzlmnkExCwhXmi

Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas