Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added a fix for an issue where we want to plug text at the beginning …

…of a textarea.
  • Loading branch information...
commit 75119b30d96643b2129f3c7e4eba77c5ca21426c 1 parent 563df29
@garyharan authored
Showing with 11 additions and 4 deletions.
  1. +3 −3 jquery.caret.js
  2. +8 −1 jquery_caret_test.html
View
6 jquery.caret.js
@@ -28,11 +28,11 @@
element.value = element.value.substr(0, start) + text + element.value.substr(end, element.value.length);
}
-
- if (start != null) {
+
+ if (start) {
setCaretTo(element, start + text.length);
} else {
- element.value += text;
+ element.value = text + element.value;
}
return this;
View
9 jquery_caret_test.html
@@ -36,7 +36,7 @@
new Test.Unit.Runner({
setup: function(){
if ($('#test_input').length == 0){
- $('<input/>').attr('id', 'test_input').appendTo(document.body).focus().css({'position': 'absolute', 'top': '-1000px'});
+ $('<textarea/>').attr('id', 'test_input').appendTo(document.body).focus().css({'position': 'absolute', 'top': '-1000px'});
}
},
@@ -47,6 +47,7 @@
testInsertAtCaret: function() { with(this) {
assertEqual('hello', $('#test_input').val('helo').setCaretPosition(2).insertAtCaret('l').val());
+ assertEqual('why hello', $('#test_input').val('hello').setCaretPosition(0).insertAtCaret('why ').val());
}},
testChaining: function() { with(this) {
@@ -56,6 +57,12 @@
testGetSelectedText: function() { with(this){
assertEqual('test', $('#test_input').val('this is a test of that').setCaretPosition(10, 14).getSelectedText());
+ }},
+
+ testTextareaInsertAtCaret: function() { with(this){
+ $('<textarea/>').attr('id', 'test_textarea').appendTo(document.body).focus().css({'position': 'absolute', 'top': '-10100px'})
+ assertEqual('this is a test', $('#test_textarea').val('is a test').setCaretPosition(0).insertAtCaret('this ').val());
+ assertEqual('this is a test', $('#test_textarea').val('this test').setCaretPosition(5).insertAtCaret('is a').val());
}}
});
// ]]>
Please sign in to comment.
Something went wrong with that request. Please try again.