Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:28:01+00:00 2026-06-13T18:28:01+00:00

I am trying to get an dynamically created element using a jQuery selector but

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:28:02+00:00Added an answer on June 13, 2026 at 6:28 pm

    Assuming thumbName is a file name, for example foo.jpg, it would not be parsed by jQuery as you would expect. .jpg part of the name is treated as a class name, and since you are not providing this class name for that element, jQuery returns an empty array – it does not find anything matching your selector. You are actually searching for an element with id foo and class name jpg.

    The way I would go with this is something along these lines:

    var packDiv = $('#templates');
    packDiv.empty();
    
    //inside a loop
    packDiv.append("<img class='" + thumbName.replace(/\./g,'') + "' src='thumbs/" + thumbName + "'/>");
    
    console.log($("."+thumbName.replace(/\./g,'')));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to remove a dynamically created element using jQuery. Here is my
I am trying to get a new dynamically created row to add to a
I'm trying to create a Window using PyGTK that has dynamically created radio buttons
I'm trying to dynamically spawn a video element on a page using JavaScript. JavaScript
I'm trying to get my current program to take information from a dynamically created
I am trying to bind the click event to an element dynamically generated using
I'm trying to get an object element from my webpage using getElementById (ultimately so
I've been trying to make an class with an array of objects created dynamically.
I am trying to get post a form to a hidden, dynamically inserted iframe,
I am trying to dynamically request a file for download via a GET ajax

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.