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
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">
<head>
<title>remplacer un élément en utilisant setAttribute</title>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
<style type="text/css"></style>
</head>
<body>
<button id="select" type="button" value="select">select</button>
<button id="input" type="button" value="input">input</button>
<form id="f" name="f" method="post">
<div id="c">
<input id="i" name="i" type="text" />
</div>
</form>
<script type="text/javascript">
var Utils = {
toSelect : function(p, e) {
var s = document.createElement("select");
var o = document.createElement("option");
var o1 = document.createTextNode("o1");
s.setAttribute("id", "s");
s.setAttribute("name", "s");
o.setAttribute("value", "o1");
o.appendChild(o1);
s.appendChild(o);
p.replaceChild(s, e);
},
toInput : function(p, e) {
var i = document.createElement("input");
i.setAttribute("id", "i");
i.setAttribute("name", "i");
i.setAttribute("type", "text");
p.replaceChild(i, e);
}
};
document.getElementById("select").onclick = function() {
if (document.getElementById("i"))
Utils.toSelect(document.getElementById("c"), document.getElementById("i"));
}
document.getElementById("input").onclick = function() {
if (document.getElementById("s"))
Utils.toInput(document.getElementById("c"), document.getElementById("s"));
}
</script>
</body>
</html> |
Partager