Blake
Blake

Reputation: 57

onclick event not working

Trying to make a image based button that will change a image on the page

Here is the code I have

<script type="text/javascript">

function nextimg()
{
alert("clicked")
document.getElementById("image").src="images/gallery/gallery-2.jpg";
}

</script>
<div class="gallery">
<table>
<tr>
<td><img src="images/lt_arrow_dark.png"/></td>
<td><img id="image" src="images/gallery/gallery-1.jpg"/></td>
<td><img onclick="nextimg()" src="images/rt_arrow_dark.png" height="55" width="24"/></td>
</tr>
</table>
</div>

Nothing happens when I click on the image. Any Ideas? Thanks

Upvotes: 2

Views: 5391

Answers (5)

Someth Victory
Someth Victory

Reputation: 4559

<script type="text/javascript">
    windown.onload = function() {
       function nextimg() {
          alert("clicked")
          document.getElementById("image").src="images/gallery/gallery-2.jpg";
       }
    }

</script>

Upvotes: 0

Rohan
Rohan

Reputation: 340

Adding the Javascript into head and the other part of the code into body, enveloped in HTML got it to work for me on Chrome and IE.

Upvotes: 0

Josh Mein
Josh Mein

Reputation: 28665

You need to remove the z-index of -2 in your css.

.gallery {
    background-color:#090909;
    position:relative;
    top:60px;
    width:100%;
}

Live DEMO

Upvotes: 0

user1431841
user1431841

Reputation: 56

I copied your code verbatim into a HTML file on my machine and it pops up the "clicked!" alert in both Chrome and Firefox. Are you sure you don't have JavaScript disabled or something? What browser are you using?

Upvotes: 1

corn3lius
corn3lius

Reputation: 5005

alert("clicked") ; //missing something

Upvotes: 2

Related Questions