OnClick type image

1

I'm having trouble making a onclick event work. I would like to change an image of an input type image. I did a test with a simple alert and it was not called the function either. Here is part of my code.

<div class="bg-secund">
  <input type="image" src="img
<div class="bg-secund">
  <input type="image" src="img%pre%.gif" id="1" onclick="click()" />
</div>

<script>
function click() {
  document.getElementById("1").src = "img/0.gif ";
}
</script>
.gif" id="1" onclick="click()" /> </div> <script> function click() { document.getElementById("1").src = "img/0.gif "; } </script>
    
asked by anonymous 06.06.2016 / 06:08

2 answers

1

A function named click() already exists in javascript, at the time it creates the handle of the onclick attribute. Try changing the function name:

<div class="bg-secund">
    <input type="image" src="img
<div class="bg-secund">
    <input type="image" src="img%pre%.gif" id="1" onclick="func()">
</div>
<script>
    function func()
    {
        document.getElementById("1").src = "img/0.gif";
    }
</script>
.gif" id="1" onclick="func()"> </div> <script> function func() { document.getElementById("1").src = "img/0.gif"; } </script>
    
06.06.2016 / 06:31
0

Vlw by the tips John and Sergio .. get it a way .. follows part of my solution at the moment. I made a game of memory with images .. several of these inputs that exchange the images according to an array of images that is randomly filled with each start of the game.

function change (i) {     var num, comp1, comp2;

if(numi == 0)
    comp1 = i;
if(numi == 1)
    comp2 = i

numi = numi + 1;

var x = imgAleatoria[i];
document.getElementById(i).src = x;

if(numi == 2){
compare(comp1, comp2);
numi = comp1 = comp2 = 0;
}

}

    
07.06.2016 / 12:30