Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

231
Vistas
How to set Image source as Input File using JavaScript?

So I want to set <img src=""> attribute using JavaScript, but the source would be a input element value.

<img src="" id="userImage" onclick="changeImage()">
<input type="file" id="imageInput">
function changeImage() {
   var userImage = document.getElementById("imageInput").value;
   document.getElementById("userImage").src = userImage;
}

Note: <input type="image"> don't work for me as it doesn't show window to select image.
When I open console, it shows

Failed to load resource:
net::ERR_UNKNOWN_URL_SCHEME
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use FileReader to read the input as a dataURL and then append that url as the image src

function read(val) {
  const reader = new FileReader();

  reader.onload = (event) => {
    document.getElementById("divOutput").src = event.target.result;
  }

  reader.readAsDataURL(val.files[0]);
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File uploader</title>
    <script src="./script.js" defer></script>
</head>

<body>
    <h1>My file uploader</h1>

    <input type='file' id='userImage' onchange = "read(this)">
    <img id='divOutput'></div>


</body>

</html>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda