AllThisOnAnACER
AllThisOnAnACER

Reputation: 331

onClick insert or append text into a textarea

I'd like to get this right without wasting any more time on posts that seem they almost answer it, but don't. Thanks for your help. I want to click Book and have the value of 'lit:' entered or appended into a text area named Discussion in which some text already resides. Please correct my errors so my four remaining brain cells can be happy. Thank you.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<php $m=1;?>

<Book>Book</Book>
<textarea name="Discussion[]" class="cleanup mceEditor" 
id="Discussion<?php echo $m;?>"><?php echo $row_review['Discussion']; ?></textarea>


<script type="text/javascript">
$(document).ready(function(){ 
   $("Book").click(function(){
      $("#Discussion<?php echo $m;$m++;?>").val('lit:');
   }); 
}); 
</script>

Upvotes: 1

Views: 1266

Answers (2)

Eric Brandel
Eric Brandel

Reputation: 880

If you're using TinyMCE (an assumption I'm making based on the mceEditor class you have in your code), you will need to do something like this:

tinyMCE.execCommand('mceInsertContent',false,'lit:');

That will insert the "lit:" text at where ever the cursor is at. You can be fancier if you want, and add in undos:

tinyMCE.execCommand('mceBeginUndoLevel');
tinyMCE.selectedInstance.execCommand('mceInsertContent', false, 'lit:');
tinyMCE.execCommand('mceEndUndoLevel');

Upvotes: 1

Supericy
Supericy

Reputation: 5896

Assuming the only issue is that you want it to append the text instead of replacing it, you could do something like this in the onclick function:

var $elm = $("#Discussion<?php echo $m;$m++;?>")
$elm.val($elm.val() + 'lit:');

This will append "lit:" to the textarea.

Upvotes: 2

Related Questions