1

I want to change src of icon used in SharePoint search template.

enter image description here

But error is coming that .attr is not a function. Am I missing something here?

  • Check whether you have referred JQuery. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> – Vishnu S Apr 18 '17 at 11:03
2

From your screenshot I see the first line $(".cbs-ItemLink img") is returning HTML as an output but it should return an Object as follows enter image description here

If it returns an object then you can use any functions of that object.

There might be 2 reasons for your issue

  1. There might be override function for $.
  2. $ is not referring your actual jQuery object.

Try to execute with following syntax to get additional information

jQuery(".cbs-ItemLink img")
jQuery(".cbs-ItemLink img").attr("src","/")
  • Thanks, this worked for me. I tried the same on ready function, DOM was not loaded so I tried it on window.load and this code works as expected. – KumarV Apr 19 '17 at 8:21
3

When jQuery says something (on an object) is not a function, the object itself (your DOM element in this case) is not there

With SP Search, the results HTML is coming in async, so you can't query for something that is not in the DOM yet

To do it right you can monitor DOM changes, but that is senior level JavaScript programming.


The easiest way out is using the window.setTimeout function to delay execution of your code and every cycle check if your required DOM element exists

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.