I have created multiple child divs within a parent div. All the divs are positioned, with the parent div set to absolute and the child divs set to relative. The z-index of the parent div is 400, while the child divs have a z-index of 500.
However, when I click on any child div, jQuery is not detecting the parent div using the click function. I am unsure why these codes are not working as expected.
If anyone could assist me in resolving this issue, it would be greatly appreciated.
ID of the Parent div: "#cardslayer"
Class of the Child divs: ".cardinlayer"
-HTML:
<body>
<div id="cardslayer"></div>
</body>
-CSS:
#cardslayer {
position: absolute;
width: 960px;
height: auto;
top: 0;
left: 0;
z-index: 400;
display: none;
}
.cardinlayer {
width: 100px;
height: 125px;
margin: 10px;
position: relative;
z-index: 500;
display: none;
}
-JQUERY: (Includes some CSS styling through jQuery)
var hazakstr = "";
var i = 0;
$("#button").click(function(){
hazakstr = "<center>";
for(i=0; i<22; i++) {
if(level_owner[i] == -1) {
hazakstr += "<div class='cardinlayer' id='house" + i + "' style='background: url(../houses/" + i + ".png); background-size: 100px 125px; background-repeat: no-repeat;'></div>";
}
}
hazakstr += "</center>";
$("#cardslayer").css("display", "block");
$("#cardslayer").html(hazakstr);
$(".cardinlayer").css("display", "inline-block");
i=((567 - $("#cardslayer").height()) / 2);
$("#cardslayer").css("height", 567 - i + "px");
$("#cardslayer").css("padding-top", i + "px");
});
HTML content added to #cardslayer after loop completion:
HTML:
<div id="cardslayer" style="display: block; height: 507px; padding-top: 60px;">
<center>
<div class="cardinlayer" id="house0" style="background: url("../houses/0.png") 0% 0% / 100px 125px no-repeat; display: inline-block;"></div>
<div class="cardinlayer" id="house1" style="background: url("../houses/1.png") 0% 0% / 100px 125px no-repeat; display: inline-block;"></div>
.
.
.
.
<div class="cardinlayer" id="house21" style="background: url("../houses/21.png") 0% 0% / 100px 125px no-repeat; display: inline-block;"></div>
</center>
</div>
Despite creating a click function for .cardinlayer, it is not functioning properly.
$(".cardinlayer").click(function(){
alert("Cardinlayer");
});
I also attempted a click function for all div elements:
$("div").click(function(){
alert($(this).attr("id"));
});
However, upon clicking a .cardinlayer element, the return value is #cardslayer instead of the specific house ID like #house1.
#cardslayer is the parent container, and .cardinlayer(s) are its children.
Image illustrating the issue: https://i.sstatic.net/QHyut.jpg
In the image, the red section represents the parent, while the blue sections represent the children. Upon clicking a card, jQuery incorrectly identifies that the faded black background (parent) has been clicked.
I seek assistance from anyone who can help me resolve this problem effectively. Thank you and have a great day!