We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

roberocity on 10/10/06


Tagged


Versions (?)


Who likes this?

4 people have marked this snippet as a favorite

shamrog12
rnrleachryan
shachi
vali29


InsertAtCursor


Published in: JavaScript 


  1. function insertAtCursor(myField, myValue) {
  2. //IE support
  3. if (document.selection) {
  4. myField.focus();
  5. sel = document.selection.createRange();
  6. sel.text = myValue;
  7. }
  8. //MOZILLA/NETSCAPE support
  9. else if (myField.selectionStart || myField.selectionStart == '0') {
  10. var startPos = myField.selectionStart;
  11. var endPos = myField.selectionEnd;
  12. myField.value = myField.value.substring(0, startPos)
  13. + myValue
  14. + myField.value.substring(endPos, myField.value.length);
  15. } else {
  16. myField.value += myValue;
  17. }
  18. }
  19.  
  20. // calling the function
  21.  
  22. insertAtCursor(document.formName.fieldName, 'this value');

Report this snippet 

You need to login to post a comment.