I want to use the 'On' function to attach a event because the others are deprecied(live, delegate,click...).
But the problem is : if we generate objects, we need to use a selector in parameters and this parameter is a string!!
Sample : (context : dynamic table)
//Wrong way
$("#dataTable tbody tr").on("click", function(event){
alert($(this).text());
});
//Good way
$("#dataTable tbody").on("click", "tr", function(event){
alert($(this).text());
});
Now How I do if I want use 'find' method in order to avoid that
// ?? (find div)
$("#dataTable tbody").on("click", "tr > ul > li > ul > li > div", function(event){
alert($(this).text());
});
// What I would like to do
$("#dataTable tbody").on("click", $(this).find("div"), function(event){
alert($(this).text());
});
//and I don't want to do this :
$("#dataTable tbody").find('div').on("click", function(event){
alert($(this).text());
});
Thank you !
"tr > ul > li > ul > li > div", just set a class on the DIV (btw, you can't have an UL directly inside a TR). Also, this smells like you're using tables for layout.on()method is a selector string. You cannot use jQuery methods here. It has to be a string. So either'ul > li > ul > li > div', or just'ul ul div', as I wrote in my answer.