Tiago Brites
Tiago Brites

Reputation: 23

Give ID's to dynamical elements

so: I put some images in a list dynamically trough the JQuery. I need to atrribute different ID's to each image because i want show the clicked image in another div.

Here's my JQ Code:

<script>

$.ajax({
    url: "Imagens/Trabalhos", //folder name
    success: function(data){
        var count = 0;
        $(data).find("a:contains(.jpg),a:contains(.png)").each(function(){
            // will loop through 
            var images = 'Imagens/Trabalhos/' +$(this).attr("href"); //get images names and set the path
            count++;
            var nome ='image'+count; //set the Suposed ID
            $('#ImageList').append('<li><a href="#" id="'+nome+'"><img src="' + images + '" width=90px height=120px></a></li>'); //Apply the images to the page, but the it dont recognise the id.

        });
    }
});

</script>

I know how to do to show the images in another div, but i need the ID (Right?). I'm not a pro on Jquery and i want to thanks all the answers.

Upvotes: 0

Views: 67

Answers (1)

PeterKA
PeterKA

Reputation: 24648

You do not need an ID to be able to show an image in another div. You just need to set up an event listener like the following:

$(function() {
    $('#ImageList').on('click', 'img', function() {
        $('#destination_div').html( $(this).clone() );
    });
});

WORKING JSFIDDLE DEMO

Upvotes: 1

Related Questions