What's new

Welcome to the forum 👋, Visitor

To access the forum content and all our services, you must register or log in to the forum. Becoming a member of the forum is completely free.

Where is the error in the code? Is there an error?

flunger96

New member
Joined
Dec 30, 2021
Messages
4
Reaction score
0
HTML Coins
0
Hello, i am new to the forum!

I have a question in JS ( am a beginner obv.).

I am programming a website (following a tutorial) and the goal is to to have a display of 5 images, when you click on one it becomes the large main image, and when you click on another, this one will become big.

Help me with the JS code in the image please. The function .onclick is not working for me. In my HTML file are 5 images with the class tag "smallimg". These should be reffered to by the var smallImg. But it is not working like it should in the image. Solution?
 

Attachments

  • jsproblem.jpg
    jsproblem.jpg
    65.5 KB · Views: 3
Last edited:
Flunger96,

Instead of using document.getElementById(), try using querySelector() for individual element selection and quwrySelectorAll() for managing element collections.
Note the difference in selection elements by their ID or Class Names

Here is an easier way to assign click events to a collection of elements ("small-img" elements). This method bypasses the need to identify the element[index) and can be instantiated on window.onload()

JavaScript:
    <div style="display: grid; width: 80%; place-content: center; padding: 2rem"><img src="images/blank.png" alt="" id="productImg" /></div>
    <div><img src="images/image-1.png" alt="" class="small-img" /></div>
    <div><img src="images/image-2.png" alt="" class="small-img" /></div>
    <div><img src="images/image-3.jpg" alt="" class="small-img" /></div>
    <script>
        var ProductImg = document.querySelector("#productImg");
        var SmallImg = document.querySelectorAll(".small-img").forEach(function (smallImage) {
            smallImage.addEventListener("click", (e) => {
                    ProductImg.src = e.currentTarget.src;
            });
        });
    </script

Hope this helps.

aCodeMonkey
 

Theme customization system

You can customize some areas of the forum theme from this menu.

  • Wide/Narrow view

    You can control a structure that you can use to use your theme wide or narrow.

    Grid view forum list

    You can control the layout of the forum list in a grid or ordinary listing style structure.

    Picture grid mode

    You can control the structure where you can open/close images in the grid forum list.

    Close sidebar

    You can get rid of the crowded view in the forum by closing the sidebar.

    Fixed sidebar

    You can make it more useful and easier to access by pinning the sidebar.

    Close radius

    You can use the radius at the corners of the blocks according to your taste by closing/opening it.

  • Choose the color combination that reflects your taste
    Background images
    Color gradient backgrounds
Back