I have this 10 elements with the class name "heroes". I made them clickable with a jQuery ".click" event. After clicking them, I made them change their classname with the ".attr" function. And they do, when I inspect them, they changed their class name to "fixheroes".
However, that doesn't stop the ".click" event to still function on them. And I don't want that.
You can find the code here:
http://codepen.io/anon/pen/KaMBmM
jQuery / JS
var counter = 1;
var picked = ["0", "0", "0", "0", "0", "0", "0"]
$(".heroes").click(function(e) {
var id = this.id;
var heroInt = id.substring(1, 3);
picked[counter] = heroInt;
var left = ["0", "21.5%", "32.9%", "44.3%", "55.7%", "67.1%", "21.5%", "32.9%", "44.3%", "55.7%", "67.1%"];
$("#" + id).attr('class', "fixheroes fix" + heroInt);
var h0 = document.getElementsByClassName('h0');
var fixnow = document.getElementsByClassName("fix" + heroInt);
fixnow[0].style.left = left[heroInt];
if (heroInt > 5) {
fixnow[0].style.top = (h0[0].offsetHeight + h0[0].getBoundingClientRect().top)
} else {
fixnow[0].style.top = "10%"
}
var ani = ".fix" + heroInt;
if (counter == 1) {
$(ani).animate({
top: "5%",
left: "3%",
}, 1000, function() {});
}
if (counter == 2) {
$(ani).animate({
left: "85.6%",
top: "5%",
}, 1000, function() {});
}
if (counter == 3) {
$(ani).animate({
left: "3%",
top: "35%",
}, 1000, function() {});
}
if (counter == 4) {
$(ani).animate({
left: "85.6%",
top: "35%",
}, 1000, function() {});
}
if (counter == 5) {
$(ani).animate({
left: "3%",
top: "65%",
}, 1000, function() {});
}
if (counter == 6) {
$(ani).animate({
left: "85.6%",
top: "65%",
}, 1000, function() {});
}
counter += 1;
});
CSS:
.h {
align-self: center;
width: 96%;
height 100%;
border-style: solid;
border-width: 0px;
border-color: 817B6F;
}
.heroes {
display: flex;
justify-content: center;
background-color: 817B6F;
position: absolute;
width: 10%;
height: auto;
padding: 0.7%;
border-width: 1px;
border-color: black;
border-style: solid;
}
.fixheroes {
display: flex;
justify-content: center;
background-color: 817B6F;
position: absolute;
width: 10%;
height: auto;
padding: 0.7%;
border-width: 1px;
border-color: black;
border-style: solid;
}
.h0 {left: -20%; top:10%}
.h1 {left: 21.5%; top:10%; border-top-left-radius: 5px;}
.h2 {left: 32.9%; top:10%;}
.h3 {left: 44.3%; top:10%;}
.h4 {left: 55.7%; top:10%;}
.h5 {left: 67.1%; top:10%; border-top-right-radius: 5px; top:10%;}
.h6 {left: 21.5%; top:50%; border-bottom-left-radius: 5px;}
.h7 {left: 32.9%; top:50%;}
.h8 {left: 44.3%; top:50%;}
.h9 {left: 55.7%; top:50%;}
.h10{left: 67.1%; top:50%; border-bottom-right-radius: 5px;}
HTML:
<div class="heroes h1" id="h1" >
<img class= "h" name= "Warrior" src="https://placehold.it/256" />
</div>
<div class="heroes h2" id="h2">
<img class= "h" name= "Warrior" src="https://placehold.it/256" />
</div>
<div class="heroes h3" id="h3">
<img class= "h" name= "Warrior" src="https://placehold.it/256" />
</div>
<div class="heroes h4" id="h4">
<img class= "h" name= "Warrior" src="https://placehold.it/256" />
</div>
<div class="heroes h5" id="h5">
<img class= "h" name= "Warrior" src="https://placehold.it/256" />
</div>
<div class="heroes h6" id="h6">
<img class= "h" name= "Warrior" src="https://placehold.it/256"/>
</div>
<div class="heroes h7" id="h7">
<img class= "h" name= "Warrior" src="https://placehold.it/256" />
</div>
<div class="heroes h8" id="h8">
<img class= "h" name= "Warrior" src="https://placehold.it/256" />
</div>
<div class="heroes h9" id="h9">
<img class= "h" name= "Warrior" src="https://placehold.it/256" />
</div>
<div class="heroes h10" id="h10">
<img class= "h" name= "Warrior" src="https://placehold.it/256" />
</div>
I made a test function where this method does work:
http://codepen.io/anon/pen/LxZrKO
So what is the problem with my code?