Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have the following issue.
By some condition I have checkbox in my web page,So
when the checkbox is disabled ,if I click on them it should show a alert box,
upon enble it should work as usual,dont show any alert.

Is it possible to bind a click event on disabled checkbox?
Posted

You can't do directly. You have to wrap it with some element and then write onclick event for that wrapper element.

Demo - http://jsfiddle.net/taditdash/rja26hcn/[^].

But this is a very weird requirement. :)
 
Share this answer
 
Comments
/\jmot 6-Dec-14 9:18am    
+5, good idea. :)
and the requirement is really weird.
Thanks /\jmot. :)
Prasanna Kumar Muduli 7-Dec-14 5:48am    
Thanks Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Welcome Prasanna. :)
Philippe Mori 10-Dec-14 10:27am    
This is because of the "very weird requirement" that I did'nt even propose a solution similar to your but instead tell him to modify his UI design and proposed 2 ways to do so.
This is a bad UI design so don't do it. Also things like that might not works the same across browser depending on how you try to work around that.

Either the control is enabled and respond to user click or the control is disabled and does not respond to user click.

If you want to tell some information to your user then write it directly on the web page next to the control or add an "Info" button next to the check box that would display the message when you click on that button.

You have to remember that users won't know that it is possible to click on a disable check box so no one will try it so you would have waste time to implement it.
 
Share this answer
 
Comments
[no name] 10-Dec-14 7:58am    
THIS IS ACTUALLY NOT POSSIBLE , BUT TRY IT AND IT'S NOT A PROPER WAY...
Correct. Take a 5. :)
Here is a checkbox overlay method you may want to consider.
CSS
.CheckBoxOverlay{
    position:relative;
    display:inline;
}
.CheckBoxOverlay div{
    position:absolute;
    left:0;
    right:0;
    top:0;
    bottom:0;
    cursor: pointer;
}

JavaScript
function showAlert(EleOverlay,CBIdent) {
    var oCB=document.getElementById(CBIdent);
    if (oCB.disabled) {
        alert("CheckBox is Disabled");
    }
    else{
        EleOverlay.style.display="none";
        oCB.checked=!oCB.checked;
    }
};

HTML
<div class="CheckBoxOverlay">
  <input id="AAA" type="checkbox" disabled="disabled" />
  <div onclick="showAlert(this,'AAA')"></div>
</div>
<div class="CheckBoxOverlay">
  <input id="BBB" type="checkbox" disabled="disabled" checked="checked" />
  <div onclick="showAlert(this,'BBB')"></div>
</div>
<div class="CheckBoxOverlay">
  <input id="CCC" type="checkbox"/>
  <div onclick="showAlert(this,'CCC')"></div>
</div>
<div class="CheckBoxOverlay">
  <input id="DDD" type="checkbox" checked="checked"/>
  <div onclick="showAlert(this,'DDD')"></div>
</div>

fiddle at: http://jsfiddle.net/dz2gdu30/[^]

fiddles to add wrapper and overlay elements:
http://jsfiddle.net/dz2gdu30/1/[^]
http://jsfiddle.net/dz2gdu30/2/[^]

modified from: http://jsfiddle.net/RXqAm/170/[^]
http://stackoverflow.com/questions/3100319/event-on-a-disabled-input[^]
and Solution 1
 
Share this answer
 
v2
XML
<html>
    <head>
        <script type="text/javascript">
            function dcl(eve)
            {
                alert("Your Message");
            }
        </script>
    </head>
    <body>
        <div onClick="dcl(this)" style=width:18px;height:16px">
            <input type="checkbox" disabled="disabled">
        </div>
    </body>
</html>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900