Onclick type image

Asked

Viewed 859 times

1

I’m having a hard time making an event onclick work. I wanted to swap an image of an image type input. I did a test with a simple Alert and also not called the function. Here’s part of my code.


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

<script>
function click() {
  document.getElementById("1").src = "img/0.gif ";
}
</script>
  • In addition to what João said, notice that right now your function changes the image to the same image. Change the name of the image you want to have...

2 answers

1

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

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

0

Vlw for the tips João and Sergio.. get one way.. follows part of my solution at the moment. I played a little memory game with pictures.. several of these inputs that exchange the images according to an image vector that is filled randomly at each start.

Function trocar(i) { var numi, 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;
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.