1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MaxLength (class)</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<style type="text/css">
<!--
-->
</style>
<script type="text/javascript">
<!--
window.onload = windowLoad;
function windowLoad() {
v_input01 = new ClassMaxLength('textarea01', 200);
v_input02 = new ClassMaxLength('textarea02', 50);
}
function ClassMaxLength(v_id, v_max) {
this.id = v_id;
this.maxlength = v_max;
this._keypress(this.id, this.maxlength);
this.prepare();
}
ClassMaxLength.prototype = {
prepare : function() {
var v_id = document.getElementById(this.id);
if(v_id.attachEvent) {
v_id.attachEvent('onkeydown', this._keypress);
v_id.attachEvent('onkeyup', this._keypress);
} else {
/* 1ier essai
v_id.addEventListener('keydown', this._keypress, false);
v_id.addEventListener('keyup', this._keypress, false);
*/
/* 2nd essai */
v_id.addEventListener('keydown', function() { ClassMaxLength.prototype._keypress(this.id, this.maxlength); }, false);
v_id.addEventListener('keyup', function() { ClassMaxLength.prototype._keypress(this.id, this.maxlength); }, false);
}
},
/* 1ier essai
_keypress : function() {
/* Ne marche pas car la variable THIS est perdue... *
var v_max = this.maxlength;
var v_id = document.getElementById(this.id);
var v_id_ = document.getElementById(this.id + '_');
if (v_id.value.length > v_max) {
v_id.value = v_id.value.substring(0, v_max);
} else {
v_id_.innerHTML = v_max - v_id.value.length;
}
}
*/
/* 2nd essai */
_keypress : function(v_id, v_max) {
// alert(v_id + ' ' + v_max);
var v_docId = document.getElementById(v_id);
var v_docId_ = document.getElementById(v_id + '_');
if (v_docId.value.length > v_max) {
v_docId.value = v_docId.value.substring(0, v_max);
} else {
v_docId_.innerHTML = v_max - v_docId.value.length;
}
}
}
-->
</script>
</head>
<body>
<div>
<h1>MaxLength (class)</h1>
<p>Il vous reste <span id="textarea01_">X</span> caractères.</p>
<p><textarea name="textarea01" id="textarea01" cols="50" rows="10">Bonjour !</textarea></p>
<hr />
<p>Il vous reste <span id="textarea02_">X</span> caractères.</p>
<p><input name="textarea02" id="textarea02" value="" /></p>
</div>
</body>
</html> |
Partager