Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var firstLineLimit = 30;
- var secondLineLimit = 55;
- $("<Textarea id>").keyup(function () {
- var value = $(this).val();
- var lines = value.split("\n");
- var totalLines = lines.length;
- var firstLine = lines[0];
- var firstLineLength = firstLine.length;
- var firstLineRest = '';
- var cursorPosition = $(this).caret().start;
- // If the first line is too long, susbtract the rest.
- if (firstLineLength > firstLineLimit) {
- // Get the rest of the first line.
- var restLength = firstLineLength - firstLineLimit;
- firstLineRest = firstLine.substr(firstLineLimit, restLength);
- // Substract the first line to its maximum length.
- firstLine = firstLine.substr(0, firstLineLimit);
- }
- // Write rest of chars for first line to screen.
- $('<wrapper id> .first-line').html(firstLineLimit - firstLine.length);
- // Remove the first line from the text part.
- var restLines = lines.splice(1, (lines.length - 1));
- // Paste the rest of the lines together as the second line.
- var secondLine = restLines.join(' ');
- // Paste the rest of the first line together with the second line.
- secondLine = firstLineRest + secondLine;
- var secondLineLength = secondLine.length;
- // If the second line is too long, susbtract the rest.
- if (secondLineLength > secondLineLimit) {
- // Substract the second line to its maximum length.
- secondLine = secondLine.substr(0, secondLineLimit);
- }
- // Write rest of chars for second line to screen.
- $('<wrapper id> .second-line').html(secondLineLimit - secondLine.length);
- // If there is a second line or there were multiple lines,
- // add the newline to the first line.
- if (secondLine.length || (totalLines > 1)) {
- // Paste first and second line together with a newline in between.
- value = firstLine + "\n" + secondLine;
- }
- // Else, just keep the first line as a value.
- else {
- value = firstLine;
- }
- // Fill in the new value.
- $(this).val(value).caret(cursorPosition, cursorPosition);
- });
Add Comment
Please, Sign In to add comment