I can't access dynamically created images by their id?!
I have a stupid problem that really bugs me...
The code below creates images based on a json feed, no problem. All images are created and the click function works perfectly, but... I can't access any of the images by their id's so I can't make the function behave as I wan't.
I first thought jQuery appended my image object as html and didn't insert it as a valid object in the dom, but as the click function is working that can't be it?
I found some other people had the same problem but they were using vanilla js, their solution was using .appendChild() but that doesn't work in jQuery.
I really hope someone can shed some light over my little issue, all input are very appreciated!
- $.each(json.hotgames, function(i, item) {
- var image = $('<img>')
- .attr('id', item.uuid+':'+item.lane)
- .attr('src', 'xxx')
- .click(function(){
- new Boxy('some html', {title: item.company_name});
- })
- .css('width', '179px')
- .css('cursor','pointer')
- .load(function() {
- arrHotGames.push('#'+item.uuid+':'+item.lane)
- if ($('#'+item.uuid+':'+item.lane).length == 0) {
- $('#hotGames').append($(image));
- } else {
- $('#'+item.uuid+':'+item.lane).replaceWith($(this));
- }
- });
- });