Can't change images based on onClick

can definition
can synonym
can noun meaning
can verb
can bus
can band
cant
can.'' acronym

I have created a small basic Javascript which will change the image with the use of arrows image. Below is my HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="styles/styles.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Alata&display=swap" rel="stylesheet">
    <title>Title</title>
</head><body>
<img class="draw" src="images/website-drawing-scan.png" alt="Main">
<h1>New idea!</h1>
<img class="left" src="images/left.png" height="50" width="40" style="float: left" alt="Left"> <img class="right" src="images/right.png" height="50" width="40" style="float: right" alt="Right">
<p>Hello! This is John with a trial test new website! Try this and you might soon be able to finish their work. </p>

<ul>
    <li>First thing to do</li>
    <li>Second thing to do</li>
    <li>Third thing to do!</li>
</ul>
</body>
<script src="scripts/main.js"></script>
</html>

And below is the script main.js

let myHeading = document.querySelector("h1");
myHeading.textContent = "Hello World!";

let myImage = document.querySelector("draw");

let left = document.querySelector("img.left");
let right = document.querySelector("img.right");

left.onclick = function () {
    if (myImage.getAttribute("src") === "images/website-drawing-scan.png") {
        alert("No more images!");
    } else {
        let width = myImage.width;
        let height = myImage.height;
        myImage.setAttribute("src", "images/firefox.png");
        myImage.setAttribute("width", width);
        myImage.setAttribute("height", height);
    }
};

right.onclick = function () {
    if (myImage.getAttribute("src") === "images/firefox.png") {
        alert("No more images!");
    } else {
        myImage.setAttribute("src", "images/website-drawing-scan.png");
    }
};

Now whenever I click on the arrows (left.png) or (right.png), nothing happens while the code is written to change the image based on the click of left arrows. Can anyone help me with this?

I suspect your selector is not working because there is no such HTML element as draw, you probably want img.draw

let myImage = document.querySelector("draw");

In general, you should be able to solve such problems yourself by verifying what your code is doing, i.e. that your variables are being populated with the correct values. A simple console.log(myImage) would have probably shown you the problem

Can, Can was the leading avant-garde rock group of the 70s. Can experimented with noise, synthesizers, non-traditional music, cut-and-paste techniques, and, most� Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.

You are trying to select an element called draw for your variable myImage. What you are looking for is an element with a class name draw. you need to change querySelector('draw') to querySelector('.draw')

I've also changed your onClick function to take arrow function, to make sure the variable are accessible from the current context.

let myHeading = document.querySelector("h1");
myHeading.textContent = "Hello World!";

let myImage = document.querySelector("img.draw");

let left = document.querySelector("img.left");
let right = document.querySelector("img.right");

left.onclick = () => {
    if (myImage.getAttribute("src") === "images/website-drawing-scan.png") {
        alert("No more images!");
    } else {
        let width = myImage.width;
        let height = myImage.height;
        myImage.setAttribute("src", "images/firefox.png");
        myImage.setAttribute("width", width);
        myImage.setAttribute("height", height);
    }
};

right.onclick = () =>  {
    if (myImage.getAttribute("src") === "images/firefox.png") {
        alert("No more images!");
    } else {
        myImage.setAttribute("src", "images/website-drawing-scan.png");
    }
};
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="styles/styles.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Alata&display=swap" rel="stylesheet">
    <title>Title</title>
</head><body>
<img class="draw" src="images/website-drawing-scan.png" alt="Main">
<h1>New idea!</h1>
<img class="left" src="images/left.png" height="50" width="40" style="float: left" alt="Left"> <img class="right" src="images/right.png" height="50" width="40" style="float: right" alt="Right">
<p>Hello! This is John with a trial test new website! Try this and you might soon be able to finish their work. </p>

<ul>
    <li>First thing to do</li>
    <li>Second thing to do</li>
    <li>Third thing to do!</li>
</ul>
</body>
<script src="scripts/main.js"></script>
</html>

Can, can noun [C] (CONTAINER). a metal container, esp. a closed, cylindrical container in which food and drink are packaged:. Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the

I don't know if it is a misprint on the JavaScript code, but you miss a dot on your query selector. It should be:

let myImage = document.querySelector(".draw");

for a valid CSS selector.

document.querySelector

The Official CAN / Spoon Records Website, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). can definition: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more.

CAN, 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.

can, can 2. (kăn). n. 1. A usually cylindrical metal container. 2. Can(stylised as CAN) was a German experimental rockband formed in Colognein 1968 by the core quartet of Holger Czukay(bass, tape editing), Irmin Schmidt(keyboards), Michael Karoli(guitar), and Jaki Liebezeit(drums).

Can, Please enter your E-mail address. If you forgot your Member details, please contact our office. This does not work for your member details. Username or email� Find the latest Canaan Inc. (CAN) stock quote, history, news and other vital information to help you with your stock trading and investing.

Comments
  • I am sure there is an error message in the console when you click