I am trying to get the value from a div pressed. It works if I use id but not class, and I don't want to spam the same line with minor differences. I want to keep the code minimal and I am new to js.
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
function test(){
var ai = document.getElementsByClassName('h').getAttribute('value');
alert(ai)
}
</script>
</head>
<body>
<div style="height:100px;width:100px;background-color:red;" class="h" value="1" onclick="test()">1</div>
<div style="height:100px;width:100px;background-color:red;" class="h" value="2" onclick="test()">2</div>
</body>
</html>