Each div is clickable (JavaScript).
How to make it such that only elements with an id are clickable, and elements with a class are not clickable?
Is there a replacement option:
var div = document.getElementsByTagName ("div");
With
var div = document.getElementsByClassName ("div");
I tried but did not work
var div = document.getElementsByTagName("div");
var divCount = div.length;
var clickedDivId
for (var i = 0; i < divCount; i += 1) {
div[i].onclick = function(e) {
clickedDivId = this.id;
event.stopPropagation();
alert(this.id);
};
}
#parent {
width: 450px;
height: 170px;
margin: 10px;
padding: 10px;
background-color: blue;
}
#child {
width: 430px;
height: 70px;
margin: 10px;
float: left;
background-color: red;
}
.parentclass {
width: 450px;
height: 170px;
margin: 10px;
padding: 10px;
background-color: blue;
}
.childclass {
width: 430px;
height: 70px;
margin: 10px;
float: left;
background-color: red;
}
<div id="parent">
<div id="child"></div>
</div>
<div class="parentclass">
<div class="childclass"></div>
</div>