Home » vue » Change element button to image

Change element button to image

Posted by: admin November 26, 2021 Leave a comment

Questions:

I want to ask how to change an element button to an image using javascript when it’s clicked? For example, from Submit button to an image of checked.

Answers:

I use svg for demo, you can change it into <img src="https://some_picture">

var button = document.getElementsByTagName("button")[0];

button.addEventListener('click', function() {
  button.innerHTML  =`<svg width="50" height="50"></svg>`
});
<button>click</button>

###

Add a click event listener to the button that changes its outerHTML property:

btn.addEventListener('click', function(){
  this.outerHTML = `<img src="https://www.gravatar.com/avatar/0fdacb141bca7fa57c392b5f03872176?s=48&d=identicon&r=PG&f=1">`
})
<button id="btn">Click</button>

###

function myFunction() {
    document.getElementById("contentChange").innerHTML = "Hello World";
}
<button id="contentChange" onclick="myFunction()">Click me</button>