-2

I'm looking for an example of code in javascript language to makes you when you're writing a text in any textarea in any website and pressed enter it makes two enter(two breaks) automatically.

for example

extension is off (default)

Hello world 1
<br>
Hello world 2

extension is on

Hello world 1
<br>
<br>
Hello world 2
felipsmartins
  • 13,269
  • 4
  • 48
  • 56
Andy
  • 21
  • 2
  • If you mean an extension for an existing framework, please mention it. If you mean that you want to give a web page the ability to add an "extension" that will allow what you're trying to do, also let us know. – Michael Todd Mar 06 '15 at 19:29
  • Please show us what you tried already, this is a website to help with bugs, not to let other people code for you – Erik Terwan Mar 06 '15 at 19:30
  • Yes the second answer I want to let the extension to add one break plus the original one – Andy Mar 06 '15 at 19:33

1 Answers1

-1

Here is this http://jsfiddle.net/5om3ahcz/1/ updated

Function:

function doubleCarriage(id){
    var a = document.getElementById(id); // grab the element
    a.addEventListener('keypress',function(e){ //create the event listener
        if(e.which == 13 || e.keyCode == 13 ) { // e.which || e.keyCode is enter
            e.preventDefault(); //prevent Default action from performing
            var caret = getCaret(a); //get our carets Position
            var length = a.value.length; //get the length of the value
            a.value = a.value.substring(0, caret) //gets our first half of string
                      + "\n\n" + //enters two breaks 
                      a.value.substring(caret, length); // gets the second half of our string.
        }
   });
}

function getCaret(el) { 
  /*
     Tells us where the caret position is 
     found at 
     http://stackoverflow.com/questions/263743/caret-position-in-textarea-in-characters-from-the-start#answer-263796
  */
  if (el.selectionStart) { 
     return el.selectionStart; 
  } else if (document.selection) { 
    el.focus(); 

    var r = document.selection.createRange(); 
    if (r == null) 
      return 0; 

   var re = el.createTextRange(), 
       rc = re.duplicate(); 
       re.moveToBookmark(r.getBookmark()); 
       rc.setEndPoint('EndToStart', re); 
   return rc.text.length; 
  }  
   return 0; 
}
//run the code like so
doubleCarriage("textarea");

Try the fiddle to see if this accomplishes what you want.

EasyBB
  • 6,176
  • 9
  • 47
  • 77
  • Why the down vote when this is precisely what is needed except for the value part? – EasyBB Mar 06 '15 at 19:40
  • I suspect because "the value part" is rather integral to a functioning textarea. With this one, type some text, put the caret in the middle and press return - is that anything like what you'd expect to happen? – James Thorpe Mar 06 '15 at 19:41
  • Well yes it is, though I needed verification if this was what the user in fact wanted. I am updating with the correct method in a moment – EasyBB Mar 06 '15 at 19:43
  • Updated with a better response I suppose – EasyBB Mar 06 '15 at 19:54