user1789462
user1789462

Reputation: 1

Is there a click() function for images?

Is there a click() function for images? Not an onClick function, but an actual click() function like buttons have

Upvotes: 0

Views: 80

Answers (5)

Pranay Rana
Pranay Rana

Reputation: 176896

EDIT

you can also try this jQuery solution

<img src="http://jsfiddle.net/img/logo.png" />

bind event like this:

$(function () {
  $("img").click(function() {
    alert('Hello World!');  
  });
});

javascript and html solution

I think that is not but you can do work around like this

<a href="#" onClick="alert('Hello World!')"><img title="The Link" /></a>

this makes your image clickable

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074028

No, images don't have a click function.

Compare the IDL for HTMLImageElement (no click listed) with the IDL for HTMLInputElement (click listed).

(Interestingly, though, the HTML5 spec — which is more recent — no longer shows a click function on HTMLInputElements. Links: HTML5 HTMLImageElement - HTML5 HTMLInputElement.)

Upvotes: 2

jeremy
jeremy

Reputation: 4314

It sounds like you are attempting to cause the action on the image. If the image performs an action on a page when you click on it, it is probably wrapped in a tag already. Without seeing the page you are working on, I would guess that you can use

 $(imageElement).parent("a").click()

Upvotes: 0

Jim Schubert
Jim Schubert

Reputation: 20357

Any reason you don't bind to the mouseup event?

http://jsfiddle.net/kR3P9/

For an image with the id "example":

function init() {
    var example = document.getElementById("example");
    example.addEventListener("mouseup", function() {
       alert("Mouse up!");
    }, false);
}

document.addEventListener("DOMContentLoaded", init, false);​

Upvotes: 0

epascarello
epascarello

Reputation: 207501

Put the image in a button.

<button><image src="foo"/></button>

You can use a background-image instead of an image tag inside.

Upvotes: 0

Related Questions