Im only a beginner and I'm not entirely sure how to from the question I have. Whenever I click on any of the "$element", only the very last "$newDiv" changes its color.
$(function () { // Document ready
test(document.getElementsByClassName("test-element"));
});
function test(divs) {
var i = 0
var l = divs.length;
for (i; i < l; i++) {
var $element = $(divs[i]);
var $newDiv = $(document.createElement("div"));
$element.after($newDiv);
$element.click(function () {
$newDiv.css({ backgroundColor: "red" });
});
}
}
How can a tweak this so that when I click on the first "$element", the only its own "$newDiv" affected? Is there a way to get around this without using DOM selectors in the event handler?