Selectionner la valeur d'une variable avec un input de type "number"

Par mowh, il y a 9 ans


Bonjour,
Tout est dans le titre, j'aimerais pouvoir choisir la valeur d'une variable dans un fichier js à côté de mon html, dans le même dossier. J'apelle donc la fonction "variable()" au changement de la valeur de mon input. Voici mon html :

<html> <head> <script language="javascript" type="text/javascript" src="script.js"></script> </head> <body> <input type="number" id="maVariable" value="3" onchange="variable()" /> </body> </html>

Et mon js :

function variable() { var maVariable = document.getElementById('maVariable').value; }

maVariable ne prend pas la valeur du input, ni même celle de base de 3. Des idées ?
Merci et bonne journée !

3 réponses

Huggy, il y a 9 ans

ton script est-il bien chargé ?
car chez moi aucun soucis

<!doctype html> <html> <head> <script> function variable() { var maVariable = document.getElementById('maVariable').value; alert(maVariable); } </script> </head> <body> <input type="number" id="maVariable" value="3" onchange="variable()" /> </body> </html>
mowh, il y a 9 ans

Oui il est chargé, mais il est sur un fichier js à part !

mowh, il y a 9 ans

En fait c'est bon, le problème ne venait pas de là mais d'un problème de variables locales/globales etc...
Merci quand même pour ta réponse Huggy !

Malo