0

I need to select an image by its' src. It is not an option in this case to select by class, id, or name. I am open to using jquery or vanilla javascript or any other method. I am certain I am entering the src properly, but the jquery selectors I am trying to use are not working. Console is not throwing any error messages.

var x = jQuery("img[src='link']");
x.hide();

I have also tried to use the src=$'link' Just to clarify I AM using a correct http: address in my actual code and it definitely matches what is showing up on the live site. Why can I not select my image by src?

  • Are you escaping the variable when including in the "img[src='link']" string? Maybe it's being broken if you're including it with no safe encoding. Try with `encodeURIComponent` method – nahuelhds May 10 '19 at 16:46
  • in my code it looks like this jQuery("img[src='http://myurl.com/img_location.jpg']"); – You_Dont_Know_Me May 10 '19 at 16:48
  • @JulianEspinosa No, I tried this method as well. I did it like jQuery('.parent').find('img[src$="http://www.location.com/test1.jpg"]'); did not work – You_Dont_Know_Me May 10 '19 at 16:52
  • Can you share any link with the full code? It's hard to help with a very generic selector – nahuelhds May 10 '19 at 20:40
  • it's not very efficient to select based on the src url. I suggest you add a class name to those particular links. – Ibu May 10 '19 at 21:24
  • Possible duplicate of [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – AuxTaco May 10 '19 at 21:27

1 Answers1

0

idk if anyone will ever read this thread, but I solved the problem by enclosing everything in $(document).ready(function(){CODE})

Of course, it wasn't finding the element because scripts load faster than html and css :p