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 8339501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:54:01+00:00 2026-06-09T04:54:01+00:00

I have a bit of jquery that makes an ajax request to a script

  • 0

I have a bit of jquery that makes an ajax request to a script and returns some json results.
These results are then parsed and a little image gallery is created.
This is then added to my form.
That bit all works perfectly….
The problem I have is that I am trying to trigger an event to the images so that when you click an image I can do some more processing…. But I just cannot get the onclick to work…

If anyone has any pointers I would be very grateful.

  $('.imgSel').click(function()
{
    alert("I WORK!!!!");
});


$("#mydiv").change(function(){ 
{
var url = "ajax_pics.php";

$.ajax({
    type: "GET",
    url: url,
    error: function(data){
        console.log("could not get get pics");
    },
    success: function(data){
        console.log(data);
        var pics = jQuery.parseJSON( data );
    $("#imageResultsDiv").remove();
    var imageResults ='<label>Image</label><div id="currImg"></div>';


    imageResults += "<div style='clear:both'></div><ul>";
    imageResults += "<div id='imageResultsDiv'><ul>";
    for(var i in pics) 
    {
        imageResults +="<li>"; 
        imageResults +="<img class='imgSel' id='"+pics[i].filename+"' src='"+pics[i].image+"'  title='"+pics[i].summary+"'>"; 
        imageResults +="<br/><small>"+pics[i].title+"</small>"; 
        imageResults +="</li>";
    }
    imageResults +="</ul>";

    $('#picsplaceholder').before(imageResults);
    }
});

});

  • 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-09T04:54:03+00:00Added an answer on June 9, 2026 at 4:54 am

    If I understood you right you are dynamically adding elements to the DOM.

    In that case you can’t use the standard click bindings as they only vind to elements already existing in the DOM.

    Try to use on(), which is the preferred method since jQuery 1.7:

    $('#mydiv').on("click", ".imgSel", function()
    {
        alert("I WORK!!!!");
    });
    

    You have to use the closest static element to bind to and then specify the sub-element you want the click event to apply to. I think in your case $('#mydiv') is the closest static element.

    If you happen to use an older version of jQuery use delegate() instead.
    See this post for more details on the different binding methods and when they were added and who replaced which and when.

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

Sidebar

Related Questions

I have a Page that expects a POST Request and returns some JSON. Essentially
With some help from SO, I have a bit of javascript/jquery that cycles through
I have some PHP AJAX code that is supposed to validate some parameters sent
Pulling my hair out here. Jquery, Multiple JSON files & Ajax I have a
I have a simple bit of jQuery which displays the row below the current
I have a very simple bit of jQuery to retrieve my latest Tweet $.getJSON(http://twitter.com/statuses/user_timeline/username.json?count=1,
simple bit of jquery. I have a div with clss linklist, inside it has
I have a javascript object with a bit of jquery for visual effect. I
I am trying to improve my jQuery skill and I have this bit of
I have problems with the following bit of javascript/jquery code: this.droppable = function(){ $('.imageWindow

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.