Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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>
share|improve this question

2 Answers 2

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.

share|improve this answer

Try this:

$("img[id$='_imgExpand']").attr("src","url/of/new/image");

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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