Is it possible to set a src attribute of an img tag in CSS?

Use content:url(“image.jpg”). Full working solution (Live Demo): <!doctype html> <style> .MyClass123{ content:url(“http://imgur.com/SZ8Cm.jpg”); } </style> <img class=”MyClass123″/> Tested and working: Chrome 14.0.835.163 Safari 4.0.5 Opera 10.6 Firefox 100 & newer Tested and Not working: FireFox 40.0.2 (observing Developer Network Tools, you can see that the URL loads, but the image is not displayed) Internet Explorer 11.0.9600.17905 … Read more

change img src on click

Your second attempt is correct. Here is the working jsFiddle: http://jsfiddle.net/MEHhs/ So the code should be: html: <div id=”picture_here”> <img src=”http://www.sbtjapan.com/img/FaceBook_img.jpg” id=”picture”/> </div> <div id=”about”> <div id=”mtl”>Mtl</div> </div> <div id=”about_2″> <div id=”contact”>SF</div> </div>​ js: $(‘#mtl’).click(function(){ $(‘#picture’).attr(‘src’, ‘http://profile.ak.fbcdn.net/hprofile-ak-ash3/41811_170099283015889_1174445894_q.jpg’); }); I’ve added some existing images found on google.

Add transition while changing img src with javascript

You want a crossfade. Basically you need to position both images on top of each other, and set one’s opacity to 0 so that it will be hidden: <div id=”container”> <img class=”hidden image1″ src=”http://www.istockphoto.com/file_thumbview_approve/4629609/2/istockphoto_4629609-green-field.jpg”> <img class=”image2″ src=”http://www.istockphoto.com/file_thumbview_approve/9958532/2/istockphoto_9958532-sun-and-clouds.jpg” /> </div> CSS: .hidden{ opacity:0; } img{ position:absolute; opacity:1; transition:opacity 0.5s linear; } With a transition set for … Read more

Javascript : get src and set as variable?

As long as the script is after the img, then: var youtubeimgsrc = document.getElementById(“youtubeimg”).src; See getElementById in the DOM specification. If the script is before the img, then of course the img doesn’t exist yet, and that doesn’t work. This is one reason why many people recommend putting scripts at the end of the body … Read more

How to get the file-path of the currently executing javascript code

Within the script: var scripts = document.getElementsByTagName(“script”), src = scripts[scripts.length-1].src; This works because the browser loads and executes scripts in order, so while your script is executing, the document it was included in is sure to have your script element as the last one on the page. This code of course must be ‘global’ to … Read more