Preserve cursor position in javascript
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
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
|
show 5 more comments
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
Can you show us yourformat()
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
|
show 5 more comments
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
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
javascript keydown cursors
edited Jan 2 at 12:31
leonardofed
asked Jan 2 at 12:26
![](https://i.stack.imgur.com/VI9Ak.png?s=32&g=1)
![](https://i.stack.imgur.com/VI9Ak.png?s=32&g=1)
leonardofedleonardofed
342114
342114
Can you show us yourformat()
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
|
show 5 more comments
Can you show us yourformat()
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
|
show 5 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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