How to set variable correctly when using "data-image"

Coreysan 1,811 Reputation points
2023-12-24T02:27:56.36+00:00

I'm working on the use of Modal in my MVC project. The View I have uses "onclick" to send a imagepath to Javascript, then the modal uses the ID value for setting the image path. All good.

However... now I have this issue when a user hovers over an image, it should display a larger image. The modal uses the following code:

                            <div class="product-zoom" data-image="imagezoom3" id="imagezoom3">
                                <img src="image3" id="image3" alt="">
                            </div>


Unfortunately, when I inspect the variable imagezoom3 for the data-image, it shows "imagezoom3" when it should be showing "\images\product_big.jpg".

Is this because of my coding, or because data-image is being used, and image paths have to be set differently?

Developer technologies | .NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Azar 29,520 Reputation points MVP Volunteer Moderator
    2023-12-24T09:47:48.1966667+00:00

    Hi

    The data-image attribute is used to store custom data associated with an element, and its value should typically be a string that represents some data related to that element.

    If you want to change the source of an image dynamically using JavaScript based on the hover effect, you should directly manipulate the src attribute of the image

    Trythis updated code

    <!-- HTML structure for the image and modal -->
    <div class="product-thumbnail">
        <img src="default_image.jpg" id="thumbnailImage" alt="Thumbnail Image" onmouseover="showLargeImage()">
    </div>
    
    <!-- Modal -->
    <div class="modal" id="myModal">
        <div class="modal-content">
            <span class="close" onclick="closeModal()">&times;</span>
            <img src="" id="largeImage" alt="Large Image">
        </div>
    </div>
    
    <!-- JavaScript to handle the image change and modal display -->
    <script>
        function showLargeImage() {
            // Get the thumbnail image element
            var thumbnailImage = document.getElementById("thumbnailImage");
    
            // Get the source of the thumbnail image
            var thumbnailSrc = thumbnailImage.src;
    
            // Set the source of the large image in the modal
            var largeImage = document.getElementById("largeImage");
            largeImage.src = thumbnailSrc;
    
            // Display the modal
            var modal = document.getElementById("myModal");
            modal.style.display = "block";
        }
    
        function closeModal() {
            // Hide the modal
            var modal = document.getElementById("myModal");
            modal.style.display = "none";
        }
    </script>
    
    
    

    If you find this helpful kindly accept the answer thanks much.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.