I have a situation where I need to toggle the selection and de-selection of images within a div. Only one image should be selected at a time, or none at all. I tried using the following code:
function handleImageSelection(img) {
$("#" + img.id).toggleClass("image_selected");
$('#snapshotsRpt > img').click(function (e) {
e.preventDefault;
$('.image_selected').removeClass('image_selected');
$(this).addClass('image_selected');
});
};
However, I noticed that at least one image remains selected at the end, which is not what I want. I want to be able to de-select an image by clicking on it again while still maintaining single select functionality.
The HTML code looks like this:
htmlBody+='<div id="snapshotsRpt">';
if (imgArray && imgArray.length>0){
for (var i=0;i<imgArray.length;i++){
htmlBody+='<img id="'+'Img'+i+'" src="'+imgArray[i].src+'" style="cursor: pointer; height:75%; width: 75%; display: block; margin: 10px; text-align: center;" class="" onclick="handleImageSelection(this);" oncontextmenu="rightClickMenu(this);"></img>';
}
}
htmlBody+='</div><div id="noScreenshot"></div>';
I'm fairly new to jQuery and would appreciate any help with resolving this issue!