/    Sign up×
Community /Pin to ProfileBookmark

reset textarea cursor position

Hello,

I have a textarea which I clear the value of using JavaScript:

textAreaRef.value=”;

This clears the textarea of previously entered values, but the cursor is left at the position of the last character entered by the user. How can I reset the cursor position to the start of the textarea?

Many thanks,

Jack

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@NatdripMay 18.2007 — textAreaRef.focus()
Copy linkTweet thisAlerts:
@woottonauthorMay 18.2007 — Tried it, didn't do the job.
Copy linkTweet thisAlerts:
@NatdripMay 18.2007 — post your code
Copy linkTweet thisAlerts:
@woottonauthorMay 18.2007 — RemoteOnChatTextAreaKeyDown = function(e)

{

// e.target is textarea

var tmp_bc_msg = e.target;

// Did the user hit return?

if (e.keyCode == 13)

{

// Yes

// Copy textarea value to another textarea

someOtherTextarea.value = tmp_bc_msg.value;

// Clear original textarea

tmp_bc_msg.value = '';

tmp_bc_msg.focus();

}

}
Copy linkTweet thisAlerts:
@woottonauthorMay 18.2007 — tmp_bc_msg is the textarea I would like the cursor postion reset, however it remains at the position of the last character entered.

RemoteOnChatTextAreaKeyDown is simply an event handler that gets triggered onkeydown.
Copy linkTweet thisAlerts:
@woottonauthorMay 18.2007 — I should also say that the textarea element is created like this:

// create textarea element

bc_msg = document.createElement("textarea");

// give it an id

bc_msg.setAttribute("id",'bc_msg');

// set its style

bc_msg.setAttribute("style",'border: 1px solid #CCCCCC; background-color: white; font-size: 13px; overflow-y: hidden; overflow-x: auto; height: 38px; width: 214px; margin: 6px;');

// add it to document body

documentBodyRef.appendChild(bc_msg);
×

Success!

Help @wootton spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...