insert text into textfield (with javascript)

hi experts :)

I have a text field with some text in it. I need the javascript to insert some text in the point of the text field where the cursor is.


<form name="form1" action="">

<input type="text" name="textfield" value="">

<a href="javascript:textfield.value='my text text text'">Insert the text</a>

The code above just replace the text in the textfield while i need to ADD it.

Thanks! 2000 points to who get the solution :)
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZvonkoSystems architectCommented:
Check this:

<a href="javascript:textfield.value+='my text text text'">Insert the text</a>

//try this
<a href="#" onclick= "document.form1.textfield.value+='my text text text';return false;">Insert the text</a>
You can also do:

<a href="#" onclick="textfield.value+='my text text text';">Insert the text</a>

no point is necessary for me.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

morpheus3gAuthor Commented:
both two adds the text always at the end of the textfield while i need to insert the text in the exactly point where the cursor is .. even if there's already text before and after...
//would work only for IE credit to  lil_puffball
var range=null;
function insert(char){if(range){range.text=char;}}
<form name="form1" action="">

<input type="text" name="textfield" value="" onkeyup="range=document.selection.createRange();" onfocus="range=document.selection.createRange();">

<a href="#" onclick="insert('my text text text');return false">Insert the text</a>


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
How can I store the caret(cursor) position of a text field/text area?
How can I insert text at the caret(cursor) position of a text field/text area?
morpheus3gAuthor Commented:
sorry but i need something cross-browser...
morpheus3gAuthor Commented:
will it get better if i use a text area instead of a text field ?
Michel PlungjanIT ExpertCommented:
Please all use return false onClick!!!
What browsers are you including in "Cross-browser".

The reason I'm asking... this is liikely not possible in netscape 4.x,  as well as earlier versions of other browsers.
morpheus3gAuthor Commented:
IE6 and Firefox
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.