Home > Mobile >  how can i change main image by clicking other thumbnail image
how can i change main image by clicking other thumbnail image

Time:09-27

as you can see I have one main image and another image below the main one but when I click any of them my main image doesn't change to any of them what can be the issue, please identify. if you can edit and send me an amende code will be helpful pls.I don't see any sort of error maybe you can find out , to be honest, i am not techy doing this to learn something new

<!doctype html>
<html lang="en">
  <head>
    <title>Title</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    
    <style>
        .small-img-group{
            display: flex;
            justify-content: space-between;
        }
        .small-img-col{
            flex-basis: 24%;
            cursor: pointer;
        }
        .counter1 {
                float: left;
                display: flex;
                justify-content: space-between;
                overflow-x: hidden;
                overflow-y: hidden;
            }

            .counter2 {
                float: left;
                display: flex;
                justify-content: space-between;
                overflow-x: hidden;
                overflow-y: hidden;
                padding-left: 15px;
            }
            
          .up, .down {
                display: inline-block;
                color: rgb(0, 0, 0);
                font-size: 20px;
                margin: 1px 1px;
                cursor: pointer;
                width: 15px;
                line-height: 14px;
                height: 16px;
                text-align: center;
                font-weight: bold;
                border: 1px solid #000;
                user-select: none;
            }
            .up:hover, .down:hover {
                
                color: #fd0b3f;
                text-align: center;
            }

            .adults {
                padding-right: 5px;
            }

            .children {
                padding-right: 5px;
            }

            input {
                appearance: none;
                height: 21px;
                text-align: center;
                width: 42px;
                line-height: 24px;
                font-size: 15px;
                border-radius: 5px;
                
            }            
        .container {
                display: flex;
            }
            input[type="radio"]{
                display: none;
            }
            label[for=private] {
                position: relative;
                color: orangered;
                font-size: 20px;
                border: 2px solid orangered;
                border-radius: 5px;
                align-items: left;
                display: flex;
                cursor: pointer;
                margin-right: 10px;
            }
            label[for=shared] {
                position: relative;
                color: orangered;
                font-size: 20px;
                border: 2px solid orangered;
                border-radius: 5px;
                align-items: left;
                display: flex;
                cursor: pointer;
            }
            input[type="radio"]:checked   label{
                background-color: orangered;
                color: white;
            }
            input[type="radio"]:checked   label:before{
                height: 16px;
                width: 16px;
                border: 10px solid white;
                background-color: orangered;
            }    
    </style>
    <script>
        function increaseCount(e, el) {
                    var input = el.previousElementSibling;
                    var value = parseInt(input.value, 10);
                    value = isNaN(value) ? 0 : value;
                    value  ;
                    input.value = value;
                }
                function decreaseCount(e, el) {
                    var input = el.nextElementSibling;
                    var value = parseInt(input.value, 10);
                    if (value > 1) {
                        value = isNaN(value) ? 0 : value;
                        value--;
                        input.value = value;
                    }
                }
                function decreaseCount2(e, el) {
                    var input = el.nextElementSibling;
                    var value = parseInt(input.value, 10);
                    if (value > 0) {
                        value = isNaN(value) ? 0 : value;
                        value--;
                        input.value = value;
                    }
                }
            </script>
            <script>
                var MainImg = document.getElementById('MainImg');
                var smallimg = document.getElementsByClassName('small-img');

                smallimg[0].onclick = function() {
                    MainImg.src = smallimg[0].src;
                }
                smallimg[1].onclick = function() {
                    MainImg.src = smallimg[1].src;
                }
                smallimg[2].onclick = function() {
                    MainImg.src = smallimg[2].src;
                }
                smallimg[3].onclick = function() {
                    MainImg.src = smallimg[3].src;
                }
            </script>
        
    
</head>
  <body>

    <section class="container sproduct my-5 pt-5">
        <div class="row mt-5">
            <div class="col-lg-5 col-md-12 col-12">
                <img class="img-fluid w-100 pb-1" src="https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg" alt="" id="MainImg" width="450">

                <div class="small-img-group">
                    <div class="small-img-col">
                        <img src="https://media.tacdn.com/media/attractions-splice-spp-674x446/09/99/99/87.jpg" width="100%" class="small-img" alt="">
                    </div>
                    <div class="small-img-col">
                        <img src="https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg" width="100%" class="small-img" alt="">
                    </div>
                    <div class="small-img-col">
                        <img src="https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg" width="100%" class="small-img" alt="">
                    </div>
                    <div class="small-img-col">
                        <img src="https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg" width="100%" class="small-img" alt="">
                    </div>
                </div>
            </div>
            <div class="col-lg-6 col-md-12 col-12">
                <h6>Home / Morning Safari</h6>
                <h3>Morning Safari</h3>
                <h2> <label> Total:</label><label class="total Price"></label> </h2>
                <div class="counter1">
                    <Label class="Adults">Adults</Label>
                    <div class='down' onclick='decreaseCount(event, this)'>-</div>
                    <input type='text' value='1' readonly>
                    <div class='up' onclick='increaseCount(event, this)'> </div>
                </div>
                <div class="counter2">
                    <Label class="Children">Children</Label>
                    <div class='down' onclick='decreaseCount2(event, this)'>-</div>
                    <input type='text' value='0' readonly>
                    <div class='up' onclick='increaseCount(event, this)'> </div>
                </div>
                <div class="container" style="padding-left: 0;padding-top: 5px;">
                    <input type="radio" name="occupancy" id="private" checked="checked">
                    <label for="private">Private</label>
                    <input type="radio" name="occupancy" id="shared">
                    <label for="shared">Shared</label>
                </div>
                
            </div>
        </div>
    </section>
      
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X 965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH 8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM B07jRM" crossorigin="anonymous"></script>
    </body>
  
</html>

CodePudding user response:

basically your script works, you just need to move your script (the one with the images onclick functions) to the bottom of your file, that way the html dom elements load on the page before the script does allowing it to attach the onclick event functions to the dom elements.

CodePudding user response:

You're trying to retrieve the img element with id "MainImg" which doesn't yet exist since your javascript code loads before your html code, so you get something like MainImg[0] is undefined... Try to put your last script (preferably all scripts) just before the closing body tag

CodePudding user response:

i have created this slider separate from your code just to show you an easy way to make one

i have used tailwind here

here's an example below hope it will help

var main = document.querySelector(".main"),
    big = main.querySelector(".big"),
    smalls = main.querySelectorAll(".each")
big.setAttribute("src",smalls[0].getAttribute("src"))
smalls.forEach(each=>{
    each.onclick = function(){
        smalls.forEach(ss=>ss.classList.remove("active"))
        each.classList.add("active")
        big.setAttribute("src",each.getAttribute("src"))
    }
})
.active{opacity:1 !important;border:3px solid gray;}
.each{opacity: 0.5;}
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">

<div class="main">
    <img class="big w-50">
    <div class="smalls flex">
        <img class="each cursor-pointer w-16 active" src="https://images.unsplash.com/photo-1557673272-b16d3b6c5a58?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1170&q=80">
        <img class="each cursor-pointer w-16" src="https://images.unsplash.com/photo-1542361345-6ef51494f677?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1170&q=80">
        <img class="each cursor-pointer w-16" src="https://images.unsplash.com/photo-1542361345-89e58247f2d5?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1170&q=80">
        <img class="each cursor-pointer w-16" src="https://images.unsplash.com/photo-1439668462223-e88f0ad95063?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1170&q=80">
    </div>
</div>

  • Related