0

Possible Duplicate:
Inserting a text where cursor is using Javascript/jquery
How do I add text to a textarea at the cursor location using javascript

I currently have a working method of adding to a textarea based off of clicking on objects with the class=".module"

My problem is its just adding to the end and I need a way for it to add to where my cursor is or something of that nature so users can add modules at anytime anywhere in the text area.

Example of my code:

Sets the value of the textarea "email-body" to be it + the module

$(".module").live('click', function() {
    $('#email-body').val($('#email-body').val()+$(this).val());
});

This is my email body & modules

<button class="module" value="1">Button 1</button>
<button class="module" value="2">Button 2</button>
    <textarea id="email-body" name="email-body"></textarea>
Community
  • 1
  • 1
Dom
  • 858
  • 2
  • 11
  • 30
  • 1
    possible dupplicate of: http://stackoverflow.com/questions/1064089/inserting-a-text-where-cursor-is-using-javascript-jquery – Peter Butkovic Jan 22 '13 at 15:46
  • @Dom I figured out a good cross-browser solution a while ago on how to set the position, if you can save where the cursor is constantly (since you're going to lose the position when you click the button) you can use this function to put it back to where it was, then fill the textarea at that position with the text you want. http://stackoverflow.com/questions/512528/set-cursor-position-in-html-textbox/12518737#12518737 – Mark Pieszak - Trilon.io Jan 22 '13 at 15:49
  • @Marc B That example uses javascript even if it does work (which I'm not sure if it does) My boss frowns on straight javascript and jquery is the standard we use. mcpDESIGNS, I have thought about this but I wasn't sure how to word the text. – Dom Jan 22 '13 at 15:53
  • jquery **IS** javascript. jquery itself does nothing for text manipulation, especially caret/text insertion. that's not its job. – Marc B Jan 22 '13 at 15:54
  • I know that, but its not the standards we are expected to use. CSS can be done inline but its looked down upon. (Not being a jerk, just trying to explain what I mean) – Dom Jan 22 '13 at 15:56

0 Answers0