MRC
MRC

Reputation: 553

If a disabled input button is clicked, display message

I have an input button, which when it is disabled, and someone tries to click it, needs to display an alert.

How is it possible to display a javascript message despite being disabled?

Tried this with no luck:

<input type="submit" onclick="alert('click')" value="Disabled Input Button" disabled/>

Upvotes: 0

Views: 10397

Answers (2)

Niels Keurentjes
Niels Keurentjes

Reputation: 41958

Use onmousedown instead of onclick, which is only fired when it is 'allowed' to. Some browsers, particularly Chrome, appear to disable all DOM events when a form element is disabled. While I think this is out of spec, you can use the following workaround:

Instead of using the disabled attribute, use CSS pointer-events to achieve a similar effect, illustrated here:

button.disabled {
    pointer-events:none;    
}

And then just use <button class="disabled"> instead of <button disabled>.

Upvotes: 4

MRC
MRC

Reputation: 553

<span  onclick="alert('This input is disabled')">
    <input type="submit" value="Disabled Input Button" disabled/>
</span>

Wrapping it with another tag that has the on click function works.

Upvotes: 2

Related Questions