Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click event on dynamically generated list items using jquery

I have a list being dynamically generated and then I click on the item and pass the index() to another function.

The problem is that this list is being populated dynamically and my code does not respond when I do click event. BUT, if I add a couple of Static li elements to the list in addition to the dynamically populated ones those Static ones work. Its very odd.

Some code:

This dynamically creates the list:

function SetOpenRecentURL( openRecentURL ) {

 $('#recentProjectsId').append('<li>' + openRecentURL + '</li>')
 }

This is the click event to pass the Index():

$('#recentProjectsId li').on('click', function () {
        var projIndex = $(this).index();
        console.log(projIndex)
        OpenProject()

    })

The HTML with a few Static Li's

<div class="recentProjects" id="recentProjectsId">
<li>Test 1</li>
<li>Test 2</li>
        </div>

When I run my program my list looks perfect and includes my static li plus my dynamic ones, but I cannot click on the dynamic ones, only static.

like image 450
Rob Avatar asked Jan 19 '13 20:01

Rob


People also ask

How can write Click event for dynamically generated button in jQuery?

$(document). ready(function() { $(document). on("click","button . delete-row",function(){ // Do something when button is clicked }); });

How to bind event dynamically in jQuery?

When we want to bind any event to an element, normally we could directly bind to any event of each element using the on() method. Example 1: This example using jQuery on() method to add paragraph element dynamically.

How to bind click event to dynamically created HTML elements in jQuery?

If you try to bind the elements that are dynamically added to the DOM using the click() method of jQuery, this will not work, because it only binds the click event to elements that exist at the time of the “binding”. To bind the click event to all existing and future elements, use jQuery's on() method.


2 Answers

When I run my program my list looks perfect and includes my static li plus my dynamic ones, but I cannot click on the dynamic ones, only static.

That's because, the way your code binds the click handler, it is only bound to elements in the page at the time that the the listener is bound. Set up the click listener just a little differently and it will work, by taking advantage of event delegation:

$('#recentProjectsId').on('click', 'li', function () {
    // snip...
});

By specifying an additional selector argument to .on():

A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element.


Note that your HTML is currently invalid. <li> elements are only valid inside of <ul>s, <ol>s, and <menu>s.

like image 181
Matt Ball Avatar answered Oct 21 '22 06:10

Matt Ball


You may use delegated events:

$("#recentProjectsId").on("click", "li", function() {
    var projIndex = $(this).index();
    console.log(projIndex)
    OpenProject()
});

Here #recentProjectsId is the parent element of <li>.

like image 29
VisioN Avatar answered Oct 21 '22 05:10

VisioN