I have a layout similar to this:
and would like to use a jQuery selector to selec
You may have 0 to many To find an element, use a To keep your code safe, use a Using
tags inside of your .find()
. .each()
. .find()
and .each()
together prevents null reference errors in the case of 0
elements while also allowing for handling of multiple
elements.// Set the click handler on your div
$("body").off("click", "#mydiv").on("click", "#mydiv", function() {
// Find the image using.find() and .each()
$(this).find("img").each(function() {
var img = this; // "this" is, now, scoped to the image element
// Do something with the image
$(this).animate({
width: ($(this).width() > 100 ? 100 : $(this).width() + 100) + "px"
}, 500);
});
});
#mydiv {
text-align: center;
vertical-align: middle;
background-color: #000000;
cursor: pointer;
padding: 50px;
}