javascriptmagnifier.js

How to get a the url of an image from id in Javascript


I know it's such a beginner thing. So I have this image in a div with the id thumb.

<img id="thumb" src="https://url-to-a-image">

And this Javascript that it's a magnify script:

<script type="text/javascript">
    var myImgSrc = document.getElementById("thumb").getElementsByTagName("img") 
[0].src;
    var evt = new Event(),
    m = new Magnifier(evt);
    m.attach({
        thumb: '#thumb',
        large: 'myImgSrc',
        largeWrapper: 'preview'
    });
</script>

As you can see I'm trying to get the image using myImgSrc and then I'm trying to use in the large: 'myImgSrc'. When I put the a fixed url in large: fixed-url-to-the-image, it works fine.


Solution

  • The element with #thumb id is the tag img it self, the current selector will not return the src value, so it should be simply:

    var myImgSrc = document.getElementById("thumb").src;