1

so I am creating a website that has dynamically added content and I want to bind on click events to the list items. The problem is I have a list item within a list item and I can not seem to get the second one to work.

HTML

<ul id="users" class="grid">
    <li>
        <figure>
            <img src="image source" alt="Title" />
            <figcaption>
                <div>
                    <h3>Title</h3>
                    <ol>
                        <li> <span>Additional Button</span> </li>
                        <li> <span>Additional Button</span> </li>
                        <li> <span>Additional Button</span> </li>
                    </ol>
                </div>
            </figcaption>
        </figure>
    </li>
</ul>

Here is the JS

$('ul#users').on("click", "li", function(e) {
    console.log($(this));
});

$('.grid ol').on("click", "li", function(e) {
    console.log($(this));
});

The first bind works without an issue but the second one is never called. Does anyone have an idea on how to get this to work. Please keep in mind that the LI elements are all dynamically created.

3
  • because this code is run in two spots, one for users and one for games... I mixed them up... should be fixed now. Commented Oct 13, 2013 at 23:43
  • Which li elements are dynamic, you have two sets of li (outer or inner). Commented Oct 13, 2013 at 23:47
  • both are generated dynamically. The Other one is the main container and the inner one is generated base on the outer one. There may be 3 inner ones or maybe even 5 Commented Oct 13, 2013 at 23:49

3 Answers 3

2

If all your li elements asre dynamically created, (ul.grid>li and ol>li) then your click handler to .grid ol > li must be assigned each time you create ol element in .grid>li.

Another way is try to change from:

$('.grid ol').on("click", "li", function(e) {
    console.log($(this));
});

to:

$('.grid').on("click", "li>ol>li", function(e) {
    console.log($(this));
});

P.S. if you dynamically create elements, then add handlers to them when they are created (if that allow application logic).

Sign up to request clarification or add additional context in comments.

2 Comments

Adding handlers when elements are created is not required to make this work.
This worked prefectly, except I had to change it from li > ol > li to ol > li
2

Simple rule to do events on dynamic content is to apply bind event on closest outer parent that is not generated dynamically.

Example:

$('.grid').on('click', 'ol>li', function(e){ 
    console.log($(this)); 
});

Just basically bind event on your not-dynamically created parent and manipulate selector inside of .on() context.

Comments

0

Your selector isn't working, change it to something like this:

$('ol li').on("click", "li", function(e) {
    console.log($(this));
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.