getelementbytagname() ne marche pas?
Bonjour les amis,
j'apprends le javascript avec un peu de css et j'ai rencontré un probléme que je comprends pas, c'est le suivant:
j'essaie de changer le style d'un bouton en faisant appel à la fonction js getelementbytagname et je ne sais pas si j'ai utilisé la bonne methode d'appel.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <html>
<head><title>JavaScript Trainning</title>
<script type="text/javascript">
function stylechange(element){
element.style.background="gray";
element.style.color="white";
element.style.width="150px";
element.style.border="solid 2px blue";
}
</script>
</head>
<body>
<input type="button" value="changer style" name="cible" onclick="stylechange(document.getElementsByName('input'))" />
</body>
</html> |
vous pouvez s'il vous plait m'expliquer pourquoi ca marche pas.
bonne journée.
Fais comme ça et sa marche j'ai testé
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <html>
<head><title>JavaScript Trainning</title>
<script type="text/javascript">
function stylechange(element){
element.style.background="gray";
element.style.color="white";
element.style.width="150px";
element.style.border="solid 2px blue";
}
</script>
</head>
<body>
<input id="monBouton" type="button" value="changer style" name="cible" onclick="stylechange(document.getElementById('monBouton'));" />
</body>
</html> |
Cette fonction retourne un tableau.
Je viens de tester. Prends en compte la notion de tableau
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <html>
<head><title>JavaScript Trainning</title>
<script type="text/javascript">
function stylechange(element){
element.style.background="gray";
element.style.color="white";
element.style.width="150px";
element.style.border="solid 2px blue";
}
</script>
</head>
<body>
<input type="button" value="changer style" name="cible" onclick="var bouton=document.getElementsByTagName('input');stylechange(bouton[0]);" />
</body>
</html> |