I am trying to use regular javaScript to create an event listener, when this is clicked, a function is called and data is passed with it. The function gets the data and returns it in an alert. In my code below, the function and alert fires on page load, not when clicked as desired.
<button data-id="777" id="dataClicker">Load Data</button>
<span id="response"></span>
<script>
var dataElement = document.getElementById('dataClicker');
dataElement.addEventListener('click', myFunction(dataElement));
function myFunction(dataElement) {
var dataId = dataElement.getAttribute('data-id');
alert(dataId);
}
</script>
I guess the more specific question is how to write something like this that is done in jQuery so easily in plain vanilla javaScript?
<div class="photos">
<img src="/img/cakes/1.jpg" alt="Patisserie JuJu" width="800" height="800" />
<img src="/img/cakes/2.jpg" alt="Patisserie JuJu" width="800" height="800" />
<img src="/img/cakes/3.jpg" alt="Patisserie JuJu" width="800" height="800" />
</div>
$(document).ready(function(){
$('.photos img').click(function(){
window.open($(this).attr('src'));
});
});