Extends the Element Type to include methods useful in managing inputs.
Tutorial/Demo
Uses String:tidy to clean up common special characters with their ASCII counterparts (smart quotes, elipse characters, stuff from MS Word, etc.).
Syntax
$('myInput').tidy();
Returns
- (element) This Element.
Returns the text of an input within a range.
Syntax
$('myInput').getTextInRange(start, end);
Arguments
- start - (number) beginning select position
- end - (number) end position
Returns
- (string) the text in the specified range
Notes
- If the range specified is out of bounds (for instance, if the text is "123" and you query for the range 2-10) the text that is found within the bounds is returned. If no text falls within the bounds, an empty string is returned.
Get the text selected in an input, returns a range (see Element:getTextInRange).
Syntax
$('myInput').getSelectedText();
Returns
- (string) The text that is selected
Notes
- If no text is selected, an empty string is returned.
Returns the index of start of the selected text.
Syntax
$('myInput').getSelectionStart();
Returns
- (number) The numerical index of the start of the selection.
Returns the index of end of the selected text.
Syntax
$('myInput').getSelectionEnd();
Returns
- (number) The numerical index of the end of the selection.
Notes
- If there is no selection, this value will be equal to Element:getSelectionStart
Returns the range of what is selected within the element.
Syntax
$('myInput').getSelectedRange();
Returns
- (object) start and end values (each an number).
Example return
{start: 2, end: 12}
Sets the caret at the given position.
Syntax
$('myInput').setCaretPosition(pos);
Arguments
- pos - (number) the location to place the caret OR "end" to place it at the end.
Returns
- (element) This Element.
Example
$('myInput').setCaretPosition(3);
$('myInput').setCaretPosition("end");
Returns the caret position.
Syntax
$('myInput').getCaretPosition();
Returns
- (number) The caret position.
Selects text within a given range.
Syntax
$('myInput').selectRange(start, end);
Arguments
- start - (number) starting number
- end - (number) ending number
Returns
- (element) This Element.
Example
$('myInput').selectRange(2, 4);
<input id="test" value="012345" />
$('test').selectRange(2, 4); //selects "23"
Inserts a value at the cursor location; if text is selected, it replaces this text.
Syntax
$('myInput').insertAtCursor(value[, selectText]);
Arguments
- value - (string) value to insert.
- selectText - (boolean) selects the text after it's been inserted
Returns
- (element) This Element.
Example
$('myInput').insertAtCursor("<br />");
$('myInput').insertAtCursor("type something here", true);
Inserts two strings around the selected text.
Syntax
$('myInput').insertAroundCursor(options);
Arguments
- options - (object) key/value set of options.
Options
- before - (string) the prefix to insert before the selected text
- after - (string) the suffix to insert after the selected text
- defaultMiddle - (string) value to insert between the prefix and the suffix if no text was selected (defaults to "SOMETHING HERE")
Returns
- (element) This Element.
Example
<input id="test" value="ninjas are the most dangerous thing in the world" />
//let's assume that the user selects the word "ninjas"
$('test').insertAroundCursor({before: "<", after: ">", defaultMiddle: "tag-name"});
//value is now:
//<ninjas> are the most dangerous thing in the world