-3

I would like to know how to add function to an image button - this code is different to other posts like mine is based around input type img not img src

*<input type="image" src="llama.png" name="saveForm" class="btTxt submit" id="saveForm" />*

I am trying to make the image into a Clicker meaning I am using a clickFunction to do so. Any suggestions?

Nova Bot
  • 1
  • 2

4 Answers4

2

Try with addEventListener():

document.getElementById('saveForm').addEventListener('click', function(){
  console.log('you have clicked', this.name);
});
<input type="image" src="llama.png" name="saveForm" class="btTxt submit" id="saveForm" />
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

You can listen to the click event by doing something like:

function foo() {
  console.log("I am called");
}
<input type="image" src="llama.png" name="saveForm" onclick="foo()" class="btTxt submit" id="saveForm" />
Ashish Ranjan
  • 12,760
  • 5
  • 27
  • 51
1
<input type="image" src="llama.png" name="saveForm" class="btTxt submit" id="saveForm" onclick="callFunction() />

<script>
callFunction(){
  alert('Calling');
}

</script>

Or You can user EventListener

document.getElementById('saveForm').addEventListener('click', function(e){
  alert('Calling');
});
Kakul Sarma
  • 303
  • 1
  • 4
  • 21
0

Here is the simple solution using onclick event

 function myImg() {
  alert("clicked");
}
 <input type="image" name="saveForm"  src="xyz.png" onclick="myImg()" class="btTxt submit" id="saveForm" />

  
Sai Manoj
  • 3,809
  • 1
  • 14
  • 35