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

  • SEARCH
  • Home
  • 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 6885609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:39:22+00:00 2026-05-27T05:39:22+00:00

I have a number of div s in my HTML file that are dynamically

  • 0

I have a number of divs in my HTML file that are dynamically generated. When I add the divs, I want to add an onclick event to each div.

Each div has a dynamic id. Using id, how can add an onclick handler?

Code that generates divs:

function createSlider(resp) {
    $.each(resp, function(key, val) {
        var item = "<div class='item' id="+val.id+">";

        item += "<h2>" + val.category + "</h2>";
        item += "<img src=" + val.image + " alt=" + val.title + ">";
        item += "</div>";

        $(".content-conveyor").append(item);
    });
}
  • 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-05-27T05:39:22+00:00Added an answer on May 27, 2026 at 5:39 am

    You can apply the click function to the ID after you append it to the dom. So, after your $(".content-conveyor").append(item); line, add this:

    $('#' + val.id).click(function() {
    // Do stuff on click
    });
    

    Edit: After thinking about it more and considering @Nemoden’s answer, you could also do it like this:

    function createSlider(resp) {
        $.each(resp, function(key, val) {
            var item = "<div class='item' id="+val.id+">";
    
            item += "<h2>" + val.category + "</h2>";
            item += "<img src=" + val.image + " alt=" + val.title + ">";
            item += "</div>";
            var $item = $(item).click(function() {
                // Add on click code here
            });
            $(".content-conveyor").append($item);
        });
    }
    

    This would attach the click callback without having to use the ID at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scrollable div element with a number of checkboxes dynamically generated. I
Hey guys quick question. I have a div that gets assigned a number to
I have a number of classes (more than 40), each one has a number
I have a page that has many posts by different authors. I want the
I have a backbone.js View that reads a template from the HTML file and
I have a printed export from an html file that it takes imports from
I have a div which contains a number of images. I would like to
I have number of line breaks in a string. But I only want it
I have a number input that should trigger jQuery on every change. To do
I have a number of classes and they are quite close to each other

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.