I created lot of child divs in parent div. All divs positioned, parent div is absolute, child divs are relative. Parent div z-index is 400, child divs are 500.
When I clicked any child div then jQuery detect parent div with clicked function. I don't understand why isn't working these codes.
So I hope anyone can help me this situation.
Parent div ID: "#cardslayer"
Child divs class: ".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: (And some style in css with jquery function.)
var hazakstr = "";
var i = 0;
$("#button").click(function(){
hazakstr = "<center>";
for(i=0; i<22; i++) {
if(level_owner[i] == -1) {
hazakstr = hazakstr + "<div class='cardinlayer' id='house" + i + "' style='background: url(../houses/" + i + ".png); background-size: 100px 125px; background-repeat: no-repeat;'></div>";
}
}
hazakstr = 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");
});
Added html to #cardslayer when loop is ended. Code like this:
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>
So after all, I created click function for .cardinlayer. And its not working.
$(".cardinlayer").click(function(){
alert("Cardinlayer");
});
I tried this click function for .cardinlayer
$("div").click(function(){
alert($(this).attr("id"));
});
When I clicked one .cardinlayer return value is #cardslayer, not #house1 or any #house.
#cardslayer is parent and .cardinlayer(s) are childs.
Picture of the problem: https://i.stack.imgur.com/QHyut.jpg
Red is parent, blue are childs. So when I click any card jquery is not detect. Jquery think I clicked the faded black background. (parent).
I hope anyone can help me. Have a nice day, and thanks.