jQuery selector after dynamically created element

Posted by Bryan Williams on Stack Overflow See other posts from Stack Overflow or by Bryan Williams
Published on 2012-10-31T16:56:15Z Indexed on 2012/10/31 17:00 UTC
Read the original article Hit count: 211

Filed under:
|

I am trying to get an dynamically created element using a jQuery selector but it is returning an empty array.

The first thing I am doing is grabbing an empty div:

var packDiv = document.getElementById('templates');
packDiv.innerHTML = "";

then adding items to it in a loop:

packDiv.innerHTML = packDiv.innerHTML + "<img id='" + thumbName + "' src='thumbs/" + thumbName + "'/>";

after the loop finishes I try to select an item using:

console.log($("#"+thumbName));

and it returns the empty array. All the things I search on show to use .on but all the examples show that is to set event handlers.

My question is how do I format a selector for dynamically created elements?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery