js/jQuery:
$('input[type=checkbox]').click(function(){
// Does not fire if I click a <input type="checkbox" disabled="disabled" />
});
How do I make something happend in jQuery when someone clicks a disabled checkbox?
js/jQuery:
$('input[type=checkbox]').click(function(){
// Does not fire if I click a <input type="checkbox" disabled="disabled" />
});
How do I make something happend in jQuery when someone clicks a disabled checkbox?
Reading over the comment again regarding using readonly
from JoãoSilva
. You could use that and connect it with some logic in the click event.
Using readonly
gives you the disabled look, just like disabled
does but it still lets you click it.
Use readonly like this:
<input type="checkbox" readonly="readonly">
Then in your script cancel the event if readonly is set.
$('input[type=checkbox]').click(function() {
var isReadOnly = $(this).attr("readonly") === undefined ? false : true;
if (isReadOnly) {
return false;
}
// other code never executed if readonly is true.
});
You will not be able to capture the click event reliably across all browsers. Your best bet is to place a transparent element above to capture the click.
HTML
<div style="display:inline-block; position:relative;">
<input type="checkbox" disabled="disabled" />
<div style="position:absolute; left:0; right:0; top:0; bottom:0;"></div>
</div>
JavaScript
$(':checkbox:disabled').next().click(function(){
var checkbox = $(this.prevNode);
// Does fire now :)
});
Note: This is an idea from this question which I improved on.
You can't...but you can fake it by placing a div over the input with a transparent background, and define the click function on that div.
$('input').each(function(){
if(true == ($(this).prop('disabled'))){
var iTop = $(this).position().top;
var iLeft = $(this).position().left;
var iWidth = $(this).width();
var iHeight = $(this).height();
$('body').append('<div class="disabledClick" style="position:absolute;top:'+iTop+'px;left:'+iLeft+'px;width:'+iWidth+'px;height:'+iHeight+'px;background:transparent;"></div>');
}
});
//delegate a click function for the 'disabledClick'.
$('body').on('click', '.disabledClick', function(){
console.log('This is the click event for the disabled checkbox.');
});
I see no other option as to add <div>
block layer over the checkbox. So the solution should be the following:
function addDisabledClickHandler(el, handler) {
$("<div />").css({
position: "absolute",
top: el.position().top,
left: el.position().left,
width: el.width(),
height: el.height()
}).click(handler).appendTo("body");
}
var el = $("input[type='checkbox']");
addDisabledClickHandler(el, function() {
alert("Clicked");
});
I actually do this quite often to stop people from changing checkboxes if those changes have already been committed to the database. However, if you want to change it or see a click at least.. then I just wrap the input in a label element and check the child input.
HTML :
<label class="checkbox-label">
<input class="input-checkbox" type="checkbox" name="declared" disabled checked>
</label>
JQuery:
$('.checkbox-label').on(function (e) {
e.stopPropagation();
let checkBox = $(this).find('.input-checkbox');
// if checkbox is disabled, do something
if (checkBox.prop('disabled')) {
// I usually put some enter password to confirm change... but I know you want to just show a click
console.log('click')
} else {
// your checkbox should act as normal
console.log('click')
}
})
Hope this helps :)