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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:16:29+00:00 2026-06-06T02:16:29+00:00

Trying to click an item in a dynamically-generated list, but the $.click() function just

  • 0

Trying to click an item in a dynamically-generated list, but the $.click() function just won’t fire.

HTML for the list:

<div>
    <ul id="site_list"> </ul>
</div>

jQuery to generate the list (which works as intended, generating a long list of data with the desired class attribute):

var sites =[];
    $.getJSON("php/ABC.php", function(result){
        $.each(result.Sites, function(i,v){
            sites.push('<li class="site" id="site_'+ ABC + '</li>');
        });

        $('#site_list').append(sites.join(''));
    });

jQuery to be triggered when clicking a list (doesn’t work…it doesn’t throw errors…just fails to respond. I’ve tried $('#site') as well, to no avail. I suspect it’s due to the method I’m using to generate the <ul>…something is preventing the DOM from properly recognizing each element inside the UL.:

$('li').click(function(){
    $(this).toggleClass("site_selected");
});
  • 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-06T02:16:31+00:00Added an answer on June 6, 2026 at 2:16 am
    $('#site_list').on('click', 'li', function(){
        $(this).toggleClass("site_selected");
    });
    

    As you’re addling li dynamically so you need delegate event handler to li.


    Note

    Delegate event structure of .on() is like following:

    $(container).on(eventName, target, handlerFunction)
    

    Here container point to a Static-element that contains the target element and belongs to DOM at page load.

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

Sidebar

Related Questions

I am trying to just click on an item in a list of items
I am trying to implement item click listener on my list in my main
I have a simple JQuery Product list. When a user click an item, it
I'm trying to dynamically create and remove items from a list, it works just
$(document).ready(function() { $('#somebutton').click(function() { $.get('List', function(responseJson) { $.each(responseJson, function(index, item) { var elem=$('<input type=checkbox
Ideally, im trying to click a div, find the next class within a parent.
I am trying to block click on links using jQuery blockUI plugin. It's seems
Trying to detect left click vs right click (without using jQuery!) and I have
I'm trying to change a Button's Click property/event when a DataTrigger is triggered but
I'm trying to make something where you can dynamically insert an HTML block, and

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.