/    Sign up×
Community /Pin to ProfileBookmark

write text at the cursor

Hi,

Using an onMouseDown, I can determine the coordinates of the cursor. Is there any way to write some text (a string) at that point. I am using at the moment a <textareaa> tag. I want the user to be able to click in the text and at a string to be inserted at that point.

Thanks for your help.
Andynic

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@andynicauthorJul 29.2011 — I found this code posted by JohnKrutsch at http://www.codingforums.com/showthread.php?t=57217

that seems to do what I want:

<html>

<head>

<script type="text/javascript">

<!--

function insert(elt,txtToIns) {

if (elt.setSelectionRange){

elt.value = elt.value.substring(0,elt.selectionStart) + txtToIns +

elt.value.substring(elt.selectionStart,elt.selectionEnd) +

elt.value.substring(elt.selectionEnd,elt.value.length);

}

else if (document.selection && document.selection.createRange) {

elt.focus();

var range = document.selection.createRange();

range.text = txtToIns + range.text;

}

}

//-->

</script>

</head>

<body>

<form>

<input type="button" value="hello" onclick="insert(this.form.ta,' hello ')">

<input type="button" value="my freind" onclick="insert(this.form.ta,' my friend ')">

<br />

<textarea rows="7" cols="30" name="ta">

This is sample text, click anywhere in here then

choose on of the buttons above to see text inserted.

</textarea>

</form>

</body>

</html>
×

Success!

Help @andynic 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...