Bonjour,
Je souhaite faire une fonction qui affiche un bloc si le client choisi "oui" dans le formulaire. Voici mon premier code :
Dans le HEAD :<script type="text/javascript" language="javascript">
function aff_div(idDiv) {
cheminChamp = document.insNe.pec;
for (i=0;i<cheminChamp.length;i++) {
if (cheminChamp[i].checked) {
checkVal=cheminChamp[i].value;
}
}
if (checkVal == 1) {
document.getElementById(idDiv).style.display = 'block';
}
else {
document.getElementById(idDiv).style.display = 'none';
}
}
</script>
Dans le BODY :
<form name="insNe" id="insNe" method="post" action="">
<div style="width:285px; height:22px; text-align:right; margin-left:4px;">
<font class="dix blanc gras">PRISE EN CHARGE<font class="rouge gras arial"> * </font>:</font>
<input type="radio" name="pec" value="1" style="vertical-align:middle;" onclick="javascript:aff_div('depVilleRam');" /><font class="dix bleu gras"> OUI</font>
<input type="radio" name="pec" value="0" style="vertical-align:middle;" onclick="javascript:aff_div('depVilleRam');" checked="checked" /><font class="dix bleu gras"> NON</font>
</div>
<div id="depVilleRam" style="float:left; width:330px; height:22px; text-align:right; margin-right:20px; display:none;">Ok ça fonctionne
</div>
</form>
Jusqu'ici tout va bien ça fonctionne très bien. Seulement j'ai voulu pousser un peu ma fonction pour pouvoir la réutiliser plus facilement : en plus de passer en paramètre l'id du bloc DIV que je veux afficher, je veux passer le nom du formulaire et le nom du champ à tester et là cela ne fonctionne plus

. Voici le code :
Dans le HEAD :
<script type="text/javascript" language="javascript">
function aff_div(nomform,nomradio,idDiv) {
cheminChamp = document+"."+nomform+"."+nomradio;
for (i=0;i<cheminChamp.length;i++) {
if (cheminChamp[i].checked) {
checkVal=cheminChamp[i].value;
}
}
if (checkVal == 1) {
document.getElementById(idDiv).style.display = 'block';
}
else {
document.getElementById(idDiv).style.display = 'none';
}
}
</script>
Dans le BODY :
<form name="insNe" id="insNe" method="post" action="">
<div style="width:285px; height:22px; text-align:right; margin-left:4px;">
<font class="dix blanc gras">PRISE EN CHARGE<font class="rouge gras arial"> * </font>:</font>
<input type="radio" name="pec" value="1" style="vertical-align:middle;" onclick="javascript:aff_div('insNe', 'pec', 'depVilleRam');" /><font class="dix bleu gras"> OUI</font>
<input type="radio" name="pec" value="0" style="vertical-align:middle;" onclick="javascript:aff_div('insNe', 'pec', 'depVilleRam');" checked="checked" /><font class="dix bleu gras"> NON</font>
</div>
<div id="depVilleRam" style="float:left; width:330px; height:22px; text-align:right; margin-right:20px; display:none;">Ok ça fonctionne
</div>
</form>
Merci de votre aide.