<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="first-block" onclick="f();" data-y="1">1</div>
<script>
var f = function() {
alert('bla-bla');
};
var secondBlock = document.createElement('div');
secondBlock.id = 'second-block';
secondBlock.innerHTML = '2';
secondBlock.setAttribute('onclick', 'f()');
secondBlock.dataset.y = '1';
document.body.appendChild(secondBlock);
</script>
</body>
</html>