I have been trying to work with a customer to create a JavaScript function in order to manipulate the amount of characters for a text box. The current issue that we are experiencing is that when starting from the first line when entering a certain amount of characters the second line is deleted. This only happens if edits are being made on a previous line. What we would like it to do is to pop the characters at the end instead or prevent adding characters after a given limit lets say 16x6. I have attempted to make edits on my own and to rewrite the code that was obtained from stackoverflow. Has anyone been able to accomplish this?
$(document).ready(function() { var textarea = document.getElementById("Field192"); //textarea.onkeyup = function() { $('.maxlinescharComments textarea').on('input focus keydown keyup', function() { var lines = textarea.value.split("\n"); var start = textarea.selectionStart; var end = textarea.selectionEnd; for (var i = 0; i < lines.length; i++) { if (lines[i].length <= 16) continue; var j = 0; space = 16; while (j++ <= 16) { if (lines[i].charAt(j) === " ") space = j; } lines[i + 1] = lines[i].substring(space + 1) + (lines[i + 1] ? " " + lines[i + 1] : ""); lines[i] = lines[i].substring(0, space); } textarea.value = lines.slice(0, 6).join("\n"); if (start == end) { textarea.setSelectionRange(start, end); } }); });
The reason for the height and the width is that the text will be imported into a PDF after the form has been submitted and we would like to ensure that the text that was input fits in the text box.