I'm working on a brief quiz that is going to be built with an iFrame in HTML and JavaScript/jQuery. I've been trying to figure out why my buttons and onclick events aren't working.
Here's what I have in HTML:
<iframe id="iframe">
</iframe>
And JS:
$(document).ready(function() {
function getDog() {
alert("Dogs are great!");
}
var $iframe = $('#iframe');
var $dogButton = "<button id=\'dog\'>Dog</button>";
$iframe.contents().find("body").append("What is your favorite animal?");
$iframe.contents().find("body").append($dogButton);
document.getElementById("dog").addEventListener("click", getDog());
})
For some reason this won't work in a snippet, so here's my CodePen: https://codepen.io/anfperez/pen/agRVKE
I know this is written in "old" JS, but I'm working with a legacy backend.
I haven't really worked with iFrames before. Can anyone give some suggestions on why this wouldn't be working?