My goal is to create multiple divs, each with a unique mousedown callback function.
However, I want each callback function to behave differently based on the specific div that is clicked.
Below is the code I have been using to create the divs and set the callbacks:
//set a specific number
var num = 4;
for(var z = 0; z < num; z++)
{
//create a div with id 'z'
$("<div/>", {id: z}).appendTo("#empty");
//displaying the id on the screen
$("#"+z).text(z);
console.log("button "+z+" created");
//Callback function that is not behaving as intended. Refer to the section below for more information
$("#"+z).mousedown(function(){
console.log("Button "+z+" clicked !");
});
}
When the above code is executed...
Clicking any of the divs results in the message "Button 4 clicked!" appearing in the console.
What changes should be made in order to achieve the desired functionality?