[V5 - Core] Click event on href created dinamically

Hello,
i have a ajax to create a list view.

ajax add multiple line like this:

<li><a href="#" onclick="

When i click the href in list view i need to start a function

How do i do ?

Thanks.

Let’s assume this happens in a page event handler, say pageInit():

// Somewhere in your routes definitions...
pageInit: function(event, page) {
  page.$el.find('li > a').click(function (e) {
  // Do something here. e.target is the element clicked
  }
}
1 Like

Refer to the immediate parent element that was already present first, then the dynamically created element,

$$("#parent-element").on("click", "li a", function(event) {
    // Your logic
});
1 Like

Hello,
i have resolved whit this cose:

self.$$(document).on("click",".class_name",function() {
   // code
    });

Because the dynamic element not fire event with this code:

$$("#parent-element").on("click", "li a", function(event) {
// Your logic

});

I have inserted my code in “pageInit”

Thank you @mossaiby and @bryceandy

Your code needs to be run after your new elements are added to the DOM. Anyway, I am glad you solved your problem!

Ok. i check the code and implement the new function. thanks! :slight_smile:

1 Like

You’re welcome, my friend!