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
|
<!DOCTYPE HTML>
<html>
<head>
<title>foo</title>
<script type="text/javascript">
function edit(theid)
{
var fieldId;
var textId;
var theField;
var theText;
// on trouve tous les textes = tous les éléments de la classe "editabe"
var textArray = document.getElementsByClassName("editable");
var i;
// pour chaque texte
for (i = 0; i < textArray.length; i++)
{
// on déduit l'id du champ correspondant au texte
textId = textArray[i].getAttribute("id");
fieldId = textId.replace("text", "field");
// on efface le champ de l'écran
theField = document.getElementById(fieldId);
theField.style.display = "none";
// on actualise le texte avec la valeur du champ correspondant
textArray[i].firstChild.nodeValue = theField.value;
textArray[i].style.display = "inline";
}
// on déduit les ids du texte et du champs correpondant au bouton cliqué
fieldId = theid.replace("button", "field");
textId = theid.replace("button", "text");
// on efface le texte de l'écran
theText = document.getElementById(textId);
theText.style.display = "none";
// on affiche le champ
theField = document.getElementById(fieldId);
theField.style.display = "inline";
}
</script>
<style>
.field
{
display: none;
}
</style>
</head>
<body>
<form>
<h3 class='editable' id='textnom'>Nom: Valjean</h3>
<input class='field' id='fieldnom' value='Valjean' />
<input type='button' value='Modifier' id='buttonnom' onclick='edit(this.id);' />
<br/><br/>
<h3 class='editable' id='textprenom'>Prénom: Jean</h3>
<input class='field' id='fieldprenom' value='Jean' />
<input type='button' value='Modifier' id='buttonprenom' onclick='edit(this.id);' />
</form>
</body>
</html> |
Partager