Jason
Jason

Reputation: 11363

jQuery unordered list element click selector issue

For a part of a web app view, the user enters in some data and it is added to an unordered list. For future modification, I want to have each list element selectable for edit and/or review.

HTML:

<div id = "answer_list_container">
  <ul id = "answers">
    <li id = "answer_1" class = "answer">Answer1 Text</li>
    <li id = "answer_2" class = "answer">Answer2 Text</li>
  </ul>
</div>

However, I can't get right selector for the list element. I've tried

answerSelectionHandler contains a console.log call that fires on execution of the handler. It is not going off, which suggest a selector issue.

Upvotes: 0

Views: 883

Answers (1)

Anton
Anton

Reputation: 32581

I'm guessing the li elements are added dynamically so you need to delegate on the closest static parent element like this

$('#answers').on('click','.answer',function(){
   answerSelectionHandler();
});

Upvotes: 6

Related Questions