Search code examples
javascriptonclicksrc

How to change src with onclick?


How change the src of a image with user input src ? Here is my current JS code ↓

function prmpt () {
  var source = prompt ("Enter Image Source ↓")
}
var image = document.getElementById("img"); function changeColor() 
{ if (image.getAttribute('src') == "https://api.sololearn.com/Uploads/Avatars/3401170.jpg") { image.src = source; } 
else { image.src = "https://api.sololearn.com/Uploads/Avatars/3401170.jpg"; } }

Solution

  • Call the below function on an input of the image source, like onblur. Assign an id to your image for which the image needs to be changed

    function imageChanger(newimage) { 
    document.getElementById("img").src=newimage; 
    }