Foros del Web » Programando para Internet » Javascript »

Problema con innerhtml

Estas en el tema de Problema con innerhtml en el foro de Javascript en Foros del Web. Hola estoy haciendo unos ejercicios para clase y cuando intento utilizar el innerhtml me sale este error: No se puede establecer la propiedad 'innerHTML' de ...
  #1 (permalink)  
Antiguo 14/12/2012, 15:16
 
Fecha de Ingreso: diciembre-2012
Mensajes: 2
Antigüedad: 11 años, 4 meses
Puntos: 0
Problema con innerhtml

Hola estoy haciendo unos ejercicios para clase y cuando intento utilizar el innerhtml me sale este error: No se puede establecer la propiedad 'innerHTML' de referencia nula o sin definir. Estoy utilizando JavaScript y el visual studio, os pongo el código:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript">

var imagenes = new Array("url(imagenes/imagen1.jpg)", "url(imagenes/imagen2.jpg)", "url(imagenes/imagen3.jpg)", "url(imagenes/imagen4.jpg)")

document.getElementById("picture").innerHTML = 'url(imagenes/imagen1.jpg)'




</script>
<style type="text/css">
#picture {
width: 356px;
margin: 180px auto auto auto;
height: 200px;
}
#boton1 {
width: 84px;
margin-left: 537px;
margin-top: 25px;
float: left;
}
#boton2 {
width: 84px;
margin-top: 25px;
float: left;
margin-left: 50px;
}
#boton3 {
width: 84px;
margin-top: 25px;
float: left;
margin-left: 51px;
}
</style>
</head>
<body">
<div id="fondo">
<div id="picture">
</div>
<div id="boton1">
<input type="button" value="Anterior" style="width: 84px" />
</div>
<div id="boton2">
<input type="button" value="Fondo" style="width: 84px" />
</div>
<div id="boton3">
<input type="button" value="Siguiente" style="width: 84px" />
</div>
</div>
</body>
</html>
  #2 (permalink)  
Antiguo 14/12/2012, 15:19
Avatar de David
Moderador
 
Fecha de Ingreso: abril-2005
Ubicación: In this planet
Mensajes: 15.720
Antigüedad: 19 años
Puntos: 839
Respuesta: Problema con innerhtml

Porque estás intentando modificar un elemento que todavía no existe. Debes hacerlo en el evento onload:
Código Javascript:
Ver original
  1. onload = function() {
  2.     // modificar elemento
  3. }
__________________
Por favor, antes de preguntar, revisa la Guía para realizar preguntas.
  #3 (permalink)  
Antiguo 14/12/2012, 15:26
 
Fecha de Ingreso: diciembre-2012
Mensajes: 2
Antigüedad: 11 años, 4 meses
Puntos: 0
Respuesta: Problema con innerhtml

Gracias David me ha funcionado.
  #4 (permalink)  
Antiguo 14/12/2012, 15:31
Avatar de David
Moderador
 
Fecha de Ingreso: abril-2005
Ubicación: In this planet
Mensajes: 15.720
Antigüedad: 19 años
Puntos: 839
Respuesta: Problema con innerhtml

De nada. Bienvenido al foro.

Saludos.
__________________
Por favor, antes de preguntar, revisa la Guía para realizar preguntas.

Etiquetas: html, innerhtml, input
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 11:37.