Teiv
Teiv

Reputation: 2635

Disable element after having been clicked?

<img src="images/get_info.png" onclick="do_ajax('get.php?id=5');this.src='images/get_info_disabled.png';" style="cursor:pointer;border:0;">

I have a list of images like this and when user clicks on it, an AJAX request will be made and the image will be changed, but how can I make the image to be clickable only once?

Upvotes: 2

Views: 2837

Answers (3)

David Thomas
David Thomas

Reputation: 253308

To allow the element to be clicked only once, the easiest way I could think of is:

<img src="path/to/image.png" onclick="alert('you clicked the image'); this.removeAttribute('onclick');" />

JS Fiddle demo of concept.

Although I'd personally add the this.removeAttribute('onclick'); into your function.

Upvotes: 0

Steven Hammons
Steven Hammons

Reputation: 1810

< p id="id">
You can do this by retreiving the element in Javascript,
< /p>
var h = documentgetbyid("id").style.display="none";

Upvotes: 0

Peter C
Peter C

Reputation: 6307

Change your do_ajax function so it looks like this:

function do_ajax(url, elem) {
    var click = elem.onclick;
    elem.onclick = null;
    // Do your Ajax stuff
    elem.onclick = click;
}

and then call it like:

<img src="images/get_info.png" onclick="do_ajax('get.php?id=5', this); this.src='images/get_info_disabled.png';" style="cursor:pointer;border:0;">

Upvotes: 5

Related Questions