Question

I am implementing a functionality called Single New Line and New Paragraph in Textarea on Google Chrome Browser (in IE my solution is working so no issue on IE because IE considers Shift+Enter (\r\n) and Enter (\n) differently). The concept is that if I presse Shift+Enter then line (texts) after cursor should be gone to the next (new) line (\r\n) but it should be considered in same paragraph. Now if I presse only Enter key then also line (texts) after cursor should be gone to next (new) line (\n) but should be considered as a New Paragraph this time.

Now my problem is: by default Textarea does not considers \r\n according to jQuery API documentation (http://api.jquery.com/val/). It only considers \n whether I press Shift+Enter or only Enter. To resolve that I implemented hook (given in jQuery site) called

$.valHooks.textarea = {
  get: function( elem ) {
    return elem.value.replace( /\r?\n/g, "\r\n" );
  }
};

But then it every times places \r\n no matters what keys I press on Textarea. I want to differentiate between Shift+Enter (\r\n) and only Enter(\n). But if above hook is placed, then every time it adds \r\n whether I press Shirt+Enter or only Enter.

On Server side now it's difficult to identified that How to consider Single New Line (\r\n) or a New Paragraph (\n)? Because in both the cases \r\n combinations are being sent from Chrome Browser.

Based on Shift+Enter and only Enter keys pressed I will differentiate them on Server side. But as explained above, every time \r\n is sent from Chrome to Server.

Above entire question is only for Google Chrome (it's working in IE's all version properly).

If still missing anything in above explanation of question please tell me.

Waiting for further assistance :)

Was it helpful?

Solution

I solved it my self. As Chrome always returns \r\n whether we press ENTER or SHIFT+ENTER. I did a trick based on my requirement.

I was in need to place a special Unicode character when User press SHIFT+ENTER and to achieve this I used below code:

jQuery(txtInputControl).val(contentOfInputControl.substring(0,caretPositionOfCursor) +  String.fromCharCode(172) + contentOfInputControl.substring(caretPositionOfCursor,contentOfInputControl.length));

Means that I place a Unicode special character in middle of a content where User has pressed SHIFT+ENTER. Also handling it a Server Side too. So that I can keep track from both side.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top