javascript - Contenteditable div with max length and jquery -


there following task - need prevent input more n symbols in contenteditable div native js / jquery, i.e. user inputs 10 symbols , can't input more, clear.

<div contenteditable="true" placeholder="Текст сообщения" class="editable ng-valid ng-valid-maxlength ng-dirty ng-valid-parse ng-touched"></div> 

this should work fiddle here

$('#editcontent').on('keydown', function(event) {    $('span').text('characters entered:' + $(this).text().length);    if ($(this).text().length === 10 && event.keycode != 8) /*delete*/ {      event.preventdefault();    }  });
body {    padding: 10px 0 0 10px;  }  #editcontent {    width: 200px;    height: 50px;    border: 1px solid blue;  }  #editcontent:after {    color: #999;    content: 'enter text';  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="editcontent" contenteditable="true"></div>  <span></span>


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -