I have learnt to change the src attribute of an image using java script in body tag. Can I do the same using java script in head tag? If yes then how?
Asked
Active
Viewed 41 times
-1
-
You need to show us your code... – Mike Sav Jul 22 '17 at 08:04
-
You can but it won't be loaded. You should take a look at this to add dynamically a script in head : https://stackoverflow.com/questions/13121948/dynamically-add-script-tag-with-src-that-may-include-document-write to add it with JS – Pierre Granger Jul 22 '17 at 08:05
-
Try to ask questions, when you think its solution isnt available on stackoverflow. Alway search before asking. Also, try to be more specific, always explain your approach and the problems you got in code. Otherwise, ye will get downvotes. Thankyou – Abdullah Danyal Jul 22 '17 at 08:06
1 Answers
0
see the snippet
<head>
<script>
function changeSrc() {
var image = document.getElementById('image');
console.log(image.src);
image.src = "secondImagesrc";
console.log(document.getElementById('image').src);
}
</script>
</head>
<body>
<img src="imagesrc" id="image"/>
<button type="button" onclick="changeSrc();">click me</button>
</body>

sumit chauhan
- 1,270
- 1
- 13
- 18