0

I try to set image src in JavaScript file whit this code :

p3GalleryBp.src = "/Uploded/foo.jpg";

when run my page, image src is empty and when inspect the image see a message in the end of image tag :

==$0

scrin shot of error

but when i use jquery it work fine, anybody know how i solve this problem?

Ali
  • 3,373
  • 5
  • 42
  • 54
  • [The `== $0` indicates you can access the element in the console using the variable `$0`](https://developer.chrome.com/devtools/docs/commandline-api#0-4). It is set as a convenience and is not related to your issue of setting an image's `src` attribute. – sdgluck May 22 '16 at 08:52

1 Answers1

0

Is variable "p3GalleryBp" defined in some way before, or you're trying to address element directly by it's ID? Seems like there should be something like:

document.getElementById("p3GalleryBp").src = "/Uploded/foo.jpg";
Maksim Volkov
  • 1,566
  • 1
  • 10
  • 4
  • 1
    Elements with an `id` attribute are available on `window` via their ID already. :-) – sdgluck May 22 '16 at 08:55
  • @sdgluck yesss...its my typo mistake ...i define it befor :) – Ali May 22 '16 at 08:56
  • @sdgluck in your opinion witch code have better speed and performance: $("#p3GalleryBp").attr("src",'foo.jpg') or p3GalleryBp.src = "/Uploded/foo.jpg"; – Ali May 22 '16 at 09:03
  • 1
    @sdgluck not always and not everywhere: http://stackoverflow.com/questions/3434278/do-dom-tree-elements-with-ids-become-global-variables ... – Maksim Volkov May 22 '16 at 09:03
  • @MaksimVolkov Thanks. :-) My comment was more a pointer to this not being the issue, rather than a suggestion that this behaviour be used. – sdgluck May 22 '16 at 09:06