Editable div with text limit

by Mohan 2014-03-21 19:16:54


Editable div with limit



Sample code for editable div with maximum limit



<html>
<head>
<script src="http://hscripts.com/jquery.js">

</script>
<script>
var MAX_TEXTINPUT = 8;
var TEXTFIELD_CLASS = "textfield";

$(document).ready(function() {

//binding keyup/down events on the contenteditable div
$('.'+TEXTFIELD_CLASS).keydown(function(e){ check_charcount(TEXTFIELD_CLASS, MAX_TEXTINPUT, e); });

})

function check_charcount(inputclass, max, e) {
var focused = $(document.activeElement)

if(focused.hasClass(inputclass) && e.which != 8 && $(focused).text().length >= max)
{
e.preventDefault();
}
}
</script>
</head>
<body>
<div contenteditable="true" name="choice1" class="textfield">sfgs</div>
</body>
</html>

1098
like
1
dislike
0
mail
flag

You must LOGIN to add comments