I have to somehow split text from textarea by rows. It's not a problem when user hit enter on it because then in string is \n char so it's easy to detect.
But what if browser automatically move some word to another row in textarea? Is browser put some special char when sentence is split automatically?
var splitted = $('#textArea').val().split("\n");
Is not a soulution because it's work only when user hit enter.
Related
here i'm copying the contents from webpage and pasting the contents in form fields like textarea. while retrieving the form contents, escape characters are also coming along which is breaking the functionality. how to avoid the escape characters and retrieve only the given text. The escape characters are not visible in the form fields, but its visible in json.
the above 1st image is entered text and second image is retrived in json. Help me out in this
Thanks.
Try to remove the line breaks with this line of code:
text = text.replace(/(\n|\r)/gm,"");
Cf: How to remove all line breaks from a string?
I have a textarea where users can input text.
If the current line starts with 3 spaces and the user hits enter, it will automatically insert 3 spaces and set the cursor right after the spaces. (there may be text before or after)
How can I detect such pattern with JavaScript?
Caret position in textarea, in characters from the start explains how to figure out where the caret is when the user hits enter so that you can check whether there are three spaces and a line break to the left.
Enter key in textarea explains how to detect the Enter key in a textarea and take an action.
Once you have a listener hooked up and know the caret location is caret, you can do something like
if (/(?:^|[\r\n]) (?:[^\r\n ][^\r\n]*)?$/
.test(myTextArea.value.substring(0, caret)) {
...
}
to take an action when there are exactly three spaces at the start of the current line.
To insert the 3 extra-spaces, you could do something like
myTextArea.value = myTextArea.value.substring(0, caret)
+ "\n " + myTextArea.value.substring(caret);
If you had a textarea, and you wanted to execute a javascript function after a pre-determined 2 character string was entered, how might you go about monitoring for that string?
A caveat is that the 2 character string might not be entered on sequential key presses.
Example:
You want to trigger an event after the 2 character string "<<" is entered. So you are typing and you enter "<", then you click somewhere else in the textarea and change some text, then come back to where you left off and put a second "<" character next to the first. The fact that there are two adjacent "<<" characters should trigger an event that can be capture by javascript. How would you monitor for and create that event?
You can check the entire textarea content onkeyup. I doubt that the textarea's content will become so big so as to make it prohibitive.
document.getElementById('textarea-id').addEventListener('keyup', function () {
this.value = this.value.replace('>>', '<<');
});
I'm using OpenMRS, it's an opensource medical records system. OpenMRS has a built-in html editor and I use this mostly in javascripting ang building the forms. In one of my forms,I have a textarea. My client would like his entries(in paragraph or in list) to be indented in the textarea.
Now when you try indenting the paragraph in the textarea then save the changes and preview the form, the paragraph becomes justified instead of retaining the indented lines.
However, if I try indenting the paragraph using ascii code for non-break space by typing or pressing alt-288, the paragraph becomes indented thus giving me the desired result. Now, the users don't prefer typing or pressing ascii equivalents coz that'll be hassle on their part.
I'm using mostly javascript and jQuery because it's what openmrs supports. If I could somehow bind the non-break space character upon pressing a key then this will work, but I'm at a lost here. How will I do this in javascript or jquery?
One solution which might work for you is to replace leading spaces in the textarea when you process/save or even each time it changes it something like :
ta.value = ta.value.replace (/\n +/, function (m) {
return '\n' + Array (m.length).join (' ');
});
The Array ... constructs creates an array containing length elements then joins with your non-breakspace character effectively creating a string of that many space chars.
Another possibility is to watch for space characters entering the text-area and transforming them. See here : Can I conditionally change the character entered into an input on keypress?
I'm trying to offer the user multi-line selectable text using a prompt() dialog. I will format the text in JavaScript, insert special characters and then throw up the prompt and all the user has to do is hit Ctrl-C, because the text is already selected.
When the prompt is displayed, the selectable text will look like this:
Line1□Line2□Line3□
where the □ is the newline character. It all gets displayed in one line, so it's easily selectable, but pasting it into Notepad or any other text editor should result in this:
Line1
Line2
Line3
I tried \r and \n and even \r\n, but it seems to truncate it to the first line.
How can I do this?
You can use Ctrl + C (at least on windows) to copy the content of a dialog box (like alert() in JavaScript).
Won't work: prompt() is single line.