I’m Rahul and I’m new to coding. I have a query related to DOM event. Please look at the following code snippet -
let door1 = document.getElementById('one');
door1.src = "closed_door.svg";
const isClicked = (door) => {
if(door.src === "closed_door.svg") {
return true;
}
else {
return false;
}
};
door1.onclick = () => {
if(isClicked(door1)) {
door1.src = "beach.svg";}
};
To give you brief, one is an id for an element. Without isClicked, I am able to successfully change the src from closed door to beach on clicking. But when I introduce isClick, it doesn’t change. Can someone please tell me what I’m missing. I’ll be very thankful
Note - I'm building game similar to this - https://s3.amazonaws.com/codecademy-content/projects/chore-door/chore-door-final/index.html They are using the same process as mine. So please suggest a solution that tells me about the error I'm making here rather than an alternative to the problem
Regards Rahul
console.log(door.src)in the isClicked method and see what it says it issetAttributeandgetAttributewhen modifying attributes that appear directly in the html. Changedoor1.src = 'closed_door.svg';intodoor1.setAttribute('src', 'closed_door.svg');. Changeif (door.src === 'closed_door.svg')intoif (door.getAttribute('src') === 'closed_door.svg')door.src.endsWith('closed_door.svg')srcon the door Element is the property of the element, not the attribute. The property most likely has the full file path. The attribute may be 'me.svg', but the property will probably be 'yoursite.com/me.svg`. This is why I said to log the value and see for yourself