user2605321
user2605321

Reputation: 93

Disabling input type="image" as button

I have an image as a button..I want to disable the image onclick...How can I do that?

<input type="image" src="someimage.png" height="20px" width="20px"  name="button" />

Upvotes: 3

Views: 13945

Answers (3)

Mihail
Mihail

Reputation: 1479

html(add id):

<input id="btn" type="image" src="someimage.png" height="20px" width="20px"  name="button" />

js:

document.getElementById("btn").onclick = function(){
  this.disabled = true
}

http://jsfiddle.net/Dtgb4/

Or you can just add attribute onclick="this.disabled = true"

update for comment:

You can use CSS rule :disabled for styling disabled buttons

http://jsfiddle.net/Dtgb4/1/

P.S. pseudo-class :disabled only for modern browsers(not for IE8-).

Upvotes: 5

rodolfoprado
rodolfoprado

Reputation: 126

You can use

<button height="20px" width="20px"  name="button"><img src="someimage.png"/></button>

and now you can disable button as normal as everything

Upvotes: 0

v2b
v2b

Reputation: 1456

Unless you have a specific reason to use the input type, you shouldn't be using it for image.

<img src="smiley.gif" alt="Smiley face" height="42" width="42">

use the image type, and no onclick handler.

Upvotes: 1

Related Questions