I'm trying to dynamically bind click listeners to divs in a for loop.
If the second jQuery selector doesn't have + i
, but is hard coded, then it works, like so:
eval(jQuery('#patient_name' + i).on('click touchstart', function(){jQuery('#taskstat0').slideToggle();}));
I also tried using two evals in the line like this:
eval(jQuery('#patient_name' + i)).on('click touchstart', function(){eval(jQuery('#taskstat' + i)).slideToggle();});
Yet this fails without console errors:
jQuery(document).ready(function(){
var patient_count = jQuery('.patient_dropdown').length;
for(var i=0; i<patient_count; i++){
eval(jQuery('#patient_name' + i).on('click touchstart', function(){jQuery('#taskstat0').slideToggle();}));
}
});
I expect each #patient_namex
to show the corresponding #taskstatx
div underneath when clicked.
What can be done to make it work?