0

I need to change the src of image using JQuery.

<a class="cbs-ItemLink" title="2E2" id="ctl00_ctl39_g_590482e6_7a32_435e_bcd5_0662381d98a8_csr1_2lines_pictureLink"> <img class="cbs-Thumbnail" src="/Style%20Library/16x16.ico" alt="2E2" id="ctl00_ctl39_g_590482e6_7a32_435e_bcd5_0662381d98a8_csr1_2lines_picture"> </a>

$("a.cbs-ItemLink img").attr('src','some value')

I have tried many things but it is giving the same error that attr is not a function.

Updated Question:-

Can you help me how to do it for all li tags like

  <li><div class="cbs-Item" data-displaytemplate="Item2Lines"><a class="cbs-ItemLink" title="2E2"><img class="cbs-Thumbnail" src="/Style%20Library/sharepoint-icon-32038-16x16.ico" alt="2E2">            </a><div class="cbs-Detail"><a class="cbs-Line1Link ms-noWrap ms-displayBlock" href="DMS/2E2" title="2E2">2E2</a></div></div</li>`
1

If attr is not a function then $("a.cbs-ItemLink img") is not returning a DOM element

Check in F12 Console what $("a.cbs-ItemLink img") returns

Possible causes why it is not working inside the page:

  • DOM element doesn't exist yet when the code executes

Vanilla JavaScript (no jQuery required)

(More typing, saves you the hassle of loading a 90 KB library)

check value in the F12 console

document.querySelector("a.cbs-ItemLink img")

Should return DOM element

assign value

document.querySelector("a.cbs-ItemLink img").src = 'some value';

  • Yes this works for one item but can you please help me with the updated question, I need it to work for all li tags – KumarV Apr 1 '17 at 12:58
  • I have used $("#ctl00_ctl39_g_590482e6_7a32_435e__csr > ul > li").each(function(){ document.querySelector("a.cbs-ItemLink img").src = '/Style%20Library/sharepoint-icon-32038-16x16.ico'; }) But this is only changing first item.. – KumarV Apr 2 '17 at 7:30
  • and also it is changing for first item when I do it from console in browser, but its not working when I have kept the code in .ready function. I have debugged also but not able to find exact reason. Please help me... – KumarV Apr 2 '17 at 7:34
  • Don't try and fix all problems in one try. Your first problem is that the DOM element is not available in the Browser in your document.ready function; fix the first problem First. Only you can fix this because only you have access to the environment. If you don't have enough JavaScript/DOM skills then you have to fix that problem first. Read blogs, see YouTube videos. It is like making a cake, if you can not make dough there is no point in trying to work with the sugar frosting. – Danny '365CSI' Engelman Apr 2 '17 at 9:52

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.