Can anybody help me with jquery code please. I need to swap this broken image in the webpart which was hardcoded please. I need to change image in the html below.

<div class="CommentsClickto">
                        <img src="//Style Library/test/img/ClicktoCommentArrow.gif" id="ctl00_PlaceHolderMain_g_47845678_682e_47e3_8d93_285fcbb8757a_ctl01_imgExpand" class="ShowPointer" alt="Expand" onclick="javascript:toggleDiv('divContent', 'ctl00_PlaceHolderMain_g_47845678_682e_47e3_8d93_285fcbb8757a_ctl01_imgExpand','ctl00_PlaceHolderMain_g_47845678_682e_47e3_8d93_285fcbb8757a_ctl01_txtComments','//Style Library/test/img/ClicktoCommentArrow.gif','//Style Library/test/img/ClicktoCommentArrowGrey.gif');" /></div>

How is that HTML being rendered? It sounds like the correct approach is to fix what's causing it to render incorrectly as opposed to bloating the page load with client side script to change it. You should never use client side script as a bandaid like that.

Since there's not necessarily a unique ID to the image (you probably don't want to use ctl00_PlaceHolderMain_g_47845678_682e_47e3_8d93_285fcbb8757a_ctl01_imgExpand), there's not necessarily a simple fix. $(".CommentsClickto img").attr('src', 'new/path/to/img.jpg'); would work, but that'd change every instance of an image within a CommentsClickto class. Not an "ideal" solution.


Try this:


Hovewer, I do support @webdes03's answer as you should first try to fix the problem which is causing the image to be corrupted. But if that's really not an option, using the above code should do the trick.

In the code, I am selecting the img tag with the jQuery "Ends With" selector and then changing the attribute of the image. Since the prefix of your id is generated server side, we cannot reference it directly. But I am guessing the id given on the server is "imgExpand" and SharePoint is generating the prefix randomly. So the end part of the id will always be _imgExpand which I have used for selection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.