Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Basically what i want to do is, when i click an image in asp image control called "subImage", the photo should be displayed in another asp image control called "brandImage". The problem is when i click the subImage, nothing happens. I try to achieve the effect using javascript and jquery. Here is the aspx code and the jquery:

What I have tried:

C#
<div id="thumbnailWrapper">
<button id="prev" class="portfolioNavigation">Previous</button>
<div id="container">
<ul id="imageList">
<li>

<asp:Repeater ID="Repeater3" runat="server" 
OnItemDataBound="subImages_ItemDataBound" ItemType="thumbnailImage" 
SelectMethod="GetThumbnail"> 
<ItemTemplate>
<asp:Image ID="subImage" runat="server" ImageUrl='<%# Item.subimages %>' 
CssClass="subPicStyle"/>
</ItemTemplate>
</asp:Repeater>

</li>
</ul>
</div>

<button id="next" class="portfolioNavigation">Next</button>
<script src="../Javascript/thumbnailNavigation.js"></script>
<script src="../Javascript/thumbnailSelection.js"></script>
</div>

<div class='guitarItemsDetailsImage'>
<asp:Image ID="brandImage" runat="server" ImageUrl='<%# Item.itemimage2 %>' 
height="900px" width="370px" />
</div>


JavaScript
var imgSrc = document.getElementById("<%=subImage.ClientID %>").src;
$(function () {
   $("#<%= subImage.ClientId %>").on("click", function (e) {
   e.preventDefault();
   $("#<%= brandImage.ClientId %>").attr("src", imgSrc);
   $("#<%= subImage.ClientId %>").attr("disabled", "disabled");
  });
});
Posted
Updated 14-Sep-17 4:35am

1 solution

Your repeater is going to have multiple images so which image do you think this is going to refer to

var imgSrc = document.getElementById("<%=subImage.ClientID %>").src;


The first one? The last one? You need to know which image is clicked so you need to add the click event to all possible images. There are many ways of doing this, below I've added a "data-image" attribute to indicate which images you want to be able to click.

<asp:Repeater ID="myRepeater" runat="server">
    <ItemTemplate>
        <asp:Image ID="subImage" runat="server" data-image ImageUrl='<%# Container.DataItem %>' CssClass="subPicStyle"/>
    </ItemTemplate>
</asp:Repeater>

<div class='guitarItemsDetailsImage'>
<asp:Image ID="brandImage" runat="server" ImageUrl='' 
height="900px" width="370px" />
</div>

<script>
    $(document).ready(function () {
        // get all images
        var images = $("[data-image]");
        images.click(function (e) {
            e.preventDefault();
            // get the image that's been clicked on
            imgSrc = $(this);

            // update brandImage to have the same src as the clicked image
            $("#<%= brandImage.ClientID %>").attr("src", imgSrc.attr('src'));

            // enable all images
            images.attr("disabled", false);

            // disable the clicked image
            imgSrc.attr("disabled", "disabled");
        });
    });
</script>
 
Share this answer
 
Comments
BebeSaiyan 15-Sep-17 2:29am    
I see..my main goal is, I want the images to automatically know what its going to refer to. This solution is interesting, I will try this when I get home. But I have a question, is the data-image and ImageUrl are on the same line? or is it just a typo?
F-ES Sitecore 15-Sep-17 4:36am    
It doesn't matter they're on the same line. The important thing is adding the data-image attribute, you can use your existing asp:Image tag and just add that attribute to it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900