Cambiar atributo src

Curso de Javascript Online. Desde cero.

14.  
19.  
25.  
34.  

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

<script>
	const cambiarImagen = () =>{
		document.querySelector('#imagen').setAttribute("src","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

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