Hey there,
I’m working on the Portfolio Project to make a Portfolio Website, and trying to get my JavaScript working on the site.
The intended purpose is for at the website load to randomly select 1 of 3 images that are hidden on the page, and unhide it.
Then, an .onclick event handler is supposed to listen for any clicks on the currently displayed image, and on click make that image disappear and randomly make a new image of the three visible.
The first random image is made visible, but the event listener isn’t being triggered when it is clicked. I can’t figure out if it’s a problem with the .onclick or a problem with the variable assignments I’m using it on with earlier functions. Looking at the Chrome Devtool events it doesn’t seem to even show the event listener on the image.
Here’s the JavaScript:
function randomPosition () {
return (Math.random() * 3);
}
let assignedPos = '';
let newPos = '';
const pos1 = document.getElementById('drone-pos1');
const pos2 = document.getElementById('drone-pos2');
const pos3 = document.getElementById('drone-pos3');
function assignPos () {
let randPos = randomPosition();
if (randPos < 1) {
newPos = pos1;
} else if (randPos < 2) {
newPos = pos2;
} else {
newPos = pos3;
}
}
function moveDrone () {
assignPos();
assignedPos.style.visibility = 'hidden';
newPos.style.visibility = 'visible';
assignedPos = newPos;
}
function loadDrone () {
assignPos();
newPos.style.visibility = 'visible';
assignedPos = newPos;
}
window.onload = loadDrone();
assignedPos.onclick = moveDrone();