I scoured the the site and found a few examples, I got close but not close enough. I have 2 checkboxes and if a user checks them they are placed in the textarea, if the user removes the check. the value is removed. I want to keep cursor position too.
I am able to add but its still clunky.
My fiddle is http://jsfiddle.net/pU2P9/18/
here is my code
Testing. Values from another field will be inserted here.
<form>
<p>Favorite Color <label><input type="checkbox" value="Green" />Green</label>
<label><input type="checkbox" value="Red" />Red</label></p>
</form>
var textarea = document.getElementById("myTextArea1");
// $('input[type=checkbox]').click(function () {
$('input[type=checkbox]').change(function () {
var $parentForm = $(this).closest("form");
// var text = $(".insert-text", $parentForm).val();
var text = $('input[type=checkbox]:checked').val() + " ";
// var text = $('input[type=checkbox]:checked', $parentForm).val() + " ";
insertAtCursor(textarea, text);
});
function insertAtCursor(myField, myValue) {
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
}
else {
myField.value += myValue;
}
}
;
Any help would be appreciated.