javascript - Add hyphen to textarea when pressed enter -



javascript - Add hyphen to textarea when pressed enter -

so want set hyphen every new line on textarea. how can accomplish this? want this:

- 1 - 2 ...

something jquery maybe have no thought what..

try this. think better

class="snippet-code-js lang-js prettyprint-override">$('#textbox').keypress(function(event){ var keycode = (event.keycode ? event.keycode : event.which); if(keycode == '13'){ //alert('you pressed "enter" key in textbox'); var cursorpos = $('#textbox').prop('selectionstart'); var v = $('#textbox').val(); var textbefore = v.substring(0, cursorpos ); var textafter = v.substring( cursorpos, v.length ); $('#textbox').val( textbefore+ "\n-" +textafter ); setcaretposition(document.getelementbyid('textbox'),( cursorpos + 2)); homecoming false; } event.stoppropagation(); }); function setcaretposition(ctrl, pos){ if(ctrl.setselectionrange) { ctrl.focus(); ctrl.setselectionrange(pos,pos); } else if (ctrl.createtextrange) { var range = ctrl.createtextrange(); range.collapse(true); range.moveend('character', pos); range.movestart('character', pos); range.select(); } } class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <textarea id="textbox"></textarea>

javascript jquery html forms textarea

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -