0
for(let feature of features){
   $("#menuList").append("<li></li>");
}
$("#menuList li").on('click', function(ev){ 
  // I need a reference to the original feature object here 
});

What is the best way to get a reference to the original feature-object within the click handler (without passing some ID as a data tag and iterating over the list again)?

Ideally, I could do something like

for(let feature of features){
   $("#menuList").append("<li></li>").on('click', function(feature){ //do something with the feature object });
}

But this does not work.

7
  • Did you try using $(document).on('click', "#menuList li", function(ev){ ... because your element is appended Commented Feb 15, 2017 at 12:58
  • What actually is feature or features here? Are you appending anything to li from it? Commented Feb 15, 2017 at 13:01
  • feature is just an object. I will definitely use some of it's attributes to build the li but I really need to pass the object to the click handler, that's the problem Commented Feb 15, 2017 at 13:02
  • 1
    Take a look here: stackoverflow.com/questions/2159368/… $.appendTo looks to be another function that will return a reference to the just appended element (not a ref to the parent element like $.append() does), and you'll be able to set that .on('click' directly, I suppose. Commented Feb 15, 2017 at 13:03
  • Thanks artur99, this looks promising! Gonna try that. Commented Feb 15, 2017 at 13:03

4 Answers 4

3

Since you are adding the li elements dynamically, you need to perform event delegation i.e. attaching click event on existing element, say #menuList here and through it navigate to clicked li and with $(this) you can get reference to clicked element through which you can get its index and get your original object from features as below:

$("#menuList").on('click','li', function(ev){ 
  var currentIndex=$(this).index();
  var feature=features[currentIndex]; //your required object from features object
});

Assuming that menuList will have fresh li appended each time.

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

Comments

1

You can access the event raising object with $(this) within the handler.

Comments

1

You can persist feature with element using $.fn.data() which can be retrieved later in the event handler.

for (let feature of features) {
  var li = $("<li></li>");
  li.data('feature', feature);
  $("#menuList").append(li);
}
$("#menuList").on('click', 'li', function(ev) {
  var feature = $(event.target).data('feature');
});

Comments

1

just use $(this)

Something like:

$('#menuList').on('click', 'li', function(ev){ 
  $(this).toggleClass('xxx');
  //console.log($(this));
});
li.xxx {
  background-color: orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul id="menuList">
  <li>one</li>
  <li>two</li>
  <li>three</li>
  <li>five</li>
</ul>

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.