How to load img in jquery

Asked

Viewed 276 times

1

Hello folks would like to know how to load image inside an append();

follows the link

append("<tr><td>"+value.codigo+"</td><td>"+value.nome+"</td><td>CARREGAR IMAGEM AQui</td><tr>");

2 answers

1

Just place inside the src of an img:

var linkImagem = 'http://lorempixel.com/image_output/abstract-q-g-640-480-4.jpg';
$("body").append(" <img src=" + linkImagem + " alt='Exemplo'> ");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>

  • jQuery(".lists-data").append("<tr><td>"+value.codigo+"</td><td>"+value.nome+"</td><td><img src="img/edit.png" class="btn-action"></td><tr>");

  • I tried the code above and it didn’t work

  • @Victormoral your problem and syntax, see the src attributes and class with double quotes, swap for single quotes that will solve

1

To create an element and insert it with jQuery you can do so:

jQuery('<img />', {
    src: "img/edit.png"
}).appendTo(td);

You can also string this code, I don’t like it but if you want it could be something like:

var table = ...
var img = '<img src="img/edit.png" class="btn-action">';
[value.codigo, value.codigo, null].forEach(function(thing, i, arr){
    var tr = document.createElement('tr');
    var td = document.createElement('td');
    td.innerHTML = i == (arr.length - 1) ? img : thing;
    tr.appendChild(td);
    table.appendChild(tr);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.