
Problemas con "document.getElementById("variable");"
Publicado por Antonio (7 intervenciones) el 21/10/2014 18:52:20
Buenas tardes, tengo el siguiente problema que no logro solucionar, pongo en situación:
Tengo un documento html con el siguiente codigo
y quiero pasar desde el cuadro de texto una variable que la recoja y la mande al documento js, la cual tiene el siguiente código:
Como se puede observar, la variable que se le pase ejecutará un switch con diversas opciones, pero lo he probado incluso con una salida "document.write" y siempre aparece que el valor de la variable es "null", ruego que atiendan mi duda, gracias.
Tengo un documento html con el siguiente codigo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Documento sin título</title>
<script type="text/javascript" src="/cursojs/js/prueba.js"> >
</script>
</head>
<body>
<FORM name="input" action="/cursojs/js/prueba.js" method="get" >
<input type="text" size="5" maxlength="1" name="variable" />
<input type="submit" value="Enviar" onclick="opcion()" />
</FORM>
</body>
</html>
y quiero pasar desde el cuadro de texto una variable que la recoja y la mande al documento js, la cual tiene el siguiente código:
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
function opcion() {
var numero = document.getElementById("variable");
switch (numero) {
case 1:
document.write('<table border=\"1\">');
document.write ('<tr>');
var num = 7;
for (var i = 0; i <=10 ; i++){
document.write ('<td>');
if ( i % 2 == 0 ){
document.write('<font color=\"red\">');
document.write( num + ' x ' + i + ' = ');
document.write( num * i );
document.write('</font>');
}
else {
document.write('<font color=\"blue\">');
document.write( num + ' x ' + i + ' = ');
document.write( num * i );
document.write('</font>');
}
document.write ('</td>');
}
document.write ('</tr>');
document.write("</table> </br> </br>");
break;
}
Como se puede observar, la variable que se le pase ejecutará un switch con diversas opciones, pero lo he probado incluso con una salida "document.write" y siempre aparece que el valor de la variable es "null", ruego que atiendan mi duda, gracias.
Valora esta pregunta


0