jQuery Style How to - Set Element max length








Question

We would like to know how to set Element max length.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.4.4.min.js'></script>
<style type='text/css'>
.long {<!--from  ww w.  j  a v a2s . c o  m-->
  width: 100px;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    $('input:text.long').attr('maxlength', parseInt($('input.long').css('width'), 10));
});
</script>
</head>
<body>
  <input type="text" class="long" />
</body>
</html>

The code above is rendered as follows: