Bonjour, voici mon code qui fonctionne en javascript. Celui ci à pour but de décompter le nombre de caracteres dans un input de type text.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
<script>
function Menu1(Target)
{
StrLen = Target.value.length
if (StrLen > 80 )
{
Target.value = Target.value.substring(0,80);
CharsLeft = 80;
}
else
{
CharsLeft = StrLen;
}
var CharsLeft1 = 80 - CharsLeft +" caractères restants";
document.frmAdmin.tit1.value = CharsLeft1;
}
 
 
 
// Je le repete encore
 
 
 
function Menu2(Target)
{
StrLen = Target.value.length
if (StrLen > 80 )
{
Target.value = Target.value.substring(0,80);
CharsLeft = 80;
}
else
{
CharsLeft = StrLen;
}
var CharsLeft1 = 80 - CharsLeft +" caractères restants";
document.frmAdmin.tit2.value = CharsLeft1;
}
 
 
 
// et encore
 
 
 
function Menu3(Target)
{
StrLen = Target.value.length
if (StrLen > 80 )
{
Target.value = Target.value.substring(0,80);
CharsLeft = 80;
}
else
{
CharsLeft = StrLen;
}
var CharsLeft1 = 80 - CharsLeft +" caractères restants";
document.frmAdmin.tit3.value = CharsLeft1;
}
 
 
 
// et encore encore...
function Menu4(Target)
{
StrLen = Target.value.length
if (StrLen > 80 )
{
Target.value = Target.value.substring(0,80);
CharsLeft = 80;
}
else
{
CharsLeft = StrLen;
}
var CharsLeft1 = 80 - CharsLeft +" caractères restants";
document.frmAdmin.tit4.value = CharsLeft1;
}
 
 
 
 
</script>
Les input sont générés grâce à la boucle "for" en php

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
   for($i=1;$i<5;$i++){
 
echo '<input name="Menu'.$i.'"  onKeyDown="Menu'.$i.'(this)" onKeyUp="Menu'.$i.'(this)" type="text" />';
// ca c'est le input compté
 
echo '<input type=text name="tit'.$i.'" readonly="readonly" />';
// ca  c'est le input qui affiche le nombre de caractere restant}

Voila cela fonctionne
Cependant je ne trouve pas comment mettre une boucle for en javascript sur le nom d'une fonction.
Si vous avez une idée pour simplifier tt ça. Merci de votre aide