Cambiar atributo src

Por 9.99€ al mes tendrás acceso completo a todos los cursos. Sin matrícula ni permanencia.

Curso de Javascript Online. Desde cero.

12.  
15.  
20.  
26.  
27.  
36.  
<script>
	const cambiarImagen = () =>{
		document.querySelector('#imagen').setAttribute("pablomonteserinHOVER.gif");
	}
	document.querySelector('#imagen').addEventListener('click', cambiarImagen);
</script>

<img src="pablomonteserin.gif"  id="imagen" width="500px" >

Ejercicio. Cambiar la imágen indefinidamente

Tendremos dos posibles aproximaciones diferentes:

i++
if(i%2==0)obj.setAttribute('src',/* ... */);
else obj.setAttribute('src', /* .... */);
foto1= !foto1;
if(foto1)obj.setAttribute('src', /* ... */);
else obj.setAttribute('src', /* ... */);

Ejemplo drag and drop

← Repaso 2
Create element →