When I click on the div element I want to alert the id of div I clicked on. But on all the div elements it is alerting the last value of array i.e. 'e1'.
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body  onload="populate();">
    <script type="text/javascript">
    function populate() {
        var divArray = ["a1", "b1", "c1", "d1", "e1"];
        for (var x in divArray) {
            if (divArray[x] === 'a1')
                document.getElementById(divArray[x]).innerHTML = "aaaaa";
            else
                document.getElementById(divArray[x]).innerHTML = "Common";
            document.getElementById(divArray[x]).onclick = function() {
                getDiv(divArray[x]);
            };
        }
    }
    function getDiv(x)
    {
        alert(x);
    }
    </script>
    <div id="a1"></div>
    <div id="b1"></div>
    <div id="c1"></div>
    <div id="d1"></div>
    <div id="e1"></div>
</body>
</html>
 
     
     
     
    