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

225
Vistas
Dynamic Img Src URL within a DIV within Script using Javascript

I am trying to insert a dynamic url within a img src.

Below is my HTML and Script code. OnClick of a tab, I am calling a GET HTTP Method, which responds with image url. I want to use this image url with the img src field.

HTML Code

        ....
        <li>
          <a href="#" class="tm-tab-link" data-id="hot" onclick="isValid();" id="login">Reviews</a>
        </li>
        ...
        <div id="hot" class="tm-tab-content">
          <div class="tm-list">
              <div class="features_items" id="name"></div>
          </div>
        </div>
        ....

Script -

$("#login").on("click", function isValid() {
    
    $.ajax({
        url: "https://xyz.foo.net/super",
        type: 'GET',
        success: function(data) {
                
            var name = "";
            for(var i=0;i<=data.length-1;i++){
            name += '<div class="tm-list-item">' +
      
                // how to add the image url below DYNAMICALLY? 
                // how to insert data[i].img ?

              '<img src="??????????" id=image1 alt="Image" class="tm-list-item-img">' +  
                        '<div class="tm-black-bg tm-list-item-text">' +
                            '<h3 class="tm-list-item-name">' + data[i].name + '</h3>' +
                            '<p class="tm-list-item-description">' + data[i].review + '</p>' +       
                        '</div> ' +
                     '</div>' 
            }
            
            $("#name").html("");
            $("#name").append(name);
        },
        error: function(e) {
            alert("Refresh Page");
        }
    });

});

Sample JSON Response -

[
 {
  "name":"John",
  "review":"awesome product",
  "img":"https://img.com/cats"
  },
  {
  "name":"Shawn",
  "review":"good product",
  "img":"https://img.com/dogs"
  }
]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

  • i just replace url with jsonplaceholder to test

  • also , id must be unique that's not releated with your problem .

    $.ajax({ url: "https://jsonplaceholder.typicode.com/photos", type: 'GET', success: function(data) {

          console.log(data);
    
          for(var i=0;i<=data.length-1;i++){
          name += '<div class="tm-list-item">' +
    
    
            '<img src="'+data[i].url+'"   id="image' + i + '" alt="Image" class="tm-list-item-img">' +  
                      '<div class="tm-black-bg tm-list-item-text">' +
                          '<h3 class="tm-list-item-name">' + data[i].name + '</h3>' +
                          '<p class="tm-list-item-description">' + data[i].review + '</p>' +       
                      '</div> ' +
                   '</div>' 
          }
    
          $("#name").html("");
          $("#name").append(name);
      },
      error: function(e) {
          alert("Refresh Page");
      }
    

    });

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use backticks ( ` ) instead of simple quotes ' or double quotes ". Backticks support string interpolation ${...} and multiline :

name += `<div class="tm-list-item">
              <img src="${data[i].img}" id="image${i}" alt="Image" class="tm-list-item-img"/>
              <div class="tm-black-bg tm-list-item-text">
                    <h3 class="tm-list-item-name">${data[i].name}</h3>
                    <p class="tm-list-item-description">${data[i].review}</p>
               </div> 
        </div>`;

EDIT :

As @AmlKamel_ said, you can't have the same ID multiple times in your document. By adding i to it, you make unique IDs.

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