How to Make a Checkbox Checked and Disabled

How can I make a checkbox readonly? not disabled?

You can easily do this by css.
HTML :

<form id="aform" name="aform" method="POST">
<input name="chkBox_1" type="checkbox" checked value="1" readonly />
<br/>
<input name="chkBox_2" type="checkbox" value="1" readonly />
<br/>
<input id="submitBttn" type="button" value="Submit">
</form>

CSS :

input[type="checkbox"][readonly] {
pointer-events: none;
}

Demo

If check box checked disable other, if unchecked enable all JavaScript?

add a simple if else statement that checks if the checkbox is checked or not. if its checked, disable all other check boxes. if not checked, enable all check boxes

<tr><td><input type="checkbox" name="progress" id="progress1" value="1" tabIndex="1" onClick="ckChange(this)">Test 1</td></tr><tr><td><input type="checkbox" name="progress" id="progress2" value="1" tabIndex="1" onClick="ckChange(this)">Test 2</td></tr><tr><td><input type="checkbox" name="progress" id="progress3" value="1" tabIndex="1" onClick="ckChange(this)">Test 3</td></tr><tr><td><input type="checkbox" name="progress" id="progress4" value="1" tabIndex="1" onClick="ckChange(this)">Test 4</td></tr>
<script>function ckChange(ckType){ var ckName = document.getElementsByName(ckType.name); var checked = document.getElementById(ckType.id);
if (checked.checked) { for(var i=0; i < ckName.length; i++){
if(!ckName[i].checked){ ckName[i].disabled = true; }else{ ckName[i].disabled = false; } } } else { for(var i=0; i < ckName.length; i++){ ckName[i].disabled = false; } } }</script>

Enable/disable all inputs and checkboxes with any checked checkbox

select all the form elements and loop through and check for id same as clicked element id.if so don't disabled it.

function enableDisableAll(e) {        var own = e;        var form = document.getElementById("myForm");        var elements = form.elements;
for (var i = 0 ; i < elements.length ; i++) { if(own !== elements[i] ){ if(own.checked == true){ elements[i].disabled = true; }else{ elements[i].disabled = false; } }
} }
function clearAll(){ document.getElementById("myForm").reset();}
<form id="myForm">
Checkbox 1: <input type="checkbox" name="checkbox1" id="checkboxOne" onclick="enableDisableAll(this);" /> <input type="text" id="id1" name="name1" /><br>
Checkbox 2: <input type="checkbox" name="checkbox2" id="checkboxTwo" onclick="enableDisableAll(this);" /> <input type="text" id="id2" name="name2" /><br>
Checkbox 3: <input type="checkbox" name="checkbox3" id="checkboxThree" onclick="enableDisableAll(this);" /> <input type="text" id="id3" name="name3" /><br>
</form>
<input class="field button2" type="button" value="Clear form" size="10" onclick="clearAll(this);">

antd disable all other Checkboxes when single selected

import { Checkbox } from 'antd';
import { useState } from 'react';

export default function App() {
const [checkList, setCheckList] = useState(['A', 'B', 'C', 'D']);
const [isSelected, setIsSelected] = useState();

const onChange = (e) => {
if (e.target.checked) {
!isSelected && setIsSelected(e.target.name);
} else {
setIsSelected(null);
}
};

return checkList.map((item) => (
<Checkbox disabled={isSelected ? isSelected !== item : false} name={item} key={item} onChange={onChange}>
{item}
</Checkbox>
));
}

You can create a state and set the checkbox key in that state. I use name attribute and assign each checkbox with item (Assuming that checkList state have unique elements or keys) get the key from the onChange Event of each checkbox and then i get the value of name from e.target + checkbox value from e.target.checked. I checked is isSelected state is not null then store the key. I each checkbox, check the disabled attribute where i check if we have any key in isSelected state and that key is not same as the checkbox key, it will be disabled otherwise it will only be selected checkbox.

Disable button if one of the checkboxes are not checked

You have to trigger change of checkboxes.

Simply checking both checkboxes have checked or not, will work only on the loading of document. You have to repeat this process each time the checkbox status is changed.

I have modified your script a little bit.

Logic

  • Select all checkboxes using document.querySelectorAll('input[type="checkbox"]').
  • Add a change event on checkbox by looping this list using forEach.
  • Inside the change event, find the count of selected checkboxes.
  • If that matches to the length of total check box, enable the button, or disable it.

const checkBoxes = document.querySelectorAll('input[type="checkbox"]');
const submitButton = document.getElementById('startusing-button');
checkBoxes.forEach((cb) => {
cb.addEventListener('change', checkButtonStatus);
});
function checkButtonStatus() {
const checkedCount = [...checkBoxes].filter((cb) => cb.checked);
submitButton.disabled = checkedCount.length !== checkBoxes.length
}
checkButtonStatus();
<input type="checkbox" id="document-checkboxid" />
<input type="checkbox" name="VAT" id="accepttermsandcond-checkbox" />

<button type="button" id="startusing-button">CreateSubscription</button>

Disable checkbox for item that is not defaultChecked

At the moment you are initializing your aSelected and bSelected states by false value, to handle your scenario you can initialize your states by props in the constructor of your component, like below example:

class App extends React.Component {
constructor(props) {
super(props);
this.state = {
aSelected: props.selectedValue == 'a',
bSelected: props.selectedValue == 'b',
};
}

handleCheckboxChange = (e) => {
const { checked, value } = e.target;
if (value == 'a') {
this.setState({ aSelected: checked });
}

if (value == 'b') {
this.setState({ bSelected: checked });
}
};

render() {
return (
<div>
<input
type="checkbox"
value="a"
onChange={this.handleCheckboxChange}
defaultChecked={this.props.selectedValue == 'a'}
disabled={
this.state.aSelected ||
(!this.state.aSelected && !this.state.bSelected)
? false
: true
}
/>

<input
type="checkbox"
value="b"
defaultChecked={this.props.selectedValue == 'b'}
onChange={this.handleCheckboxChange}
disabled={
this.state.bSelected ||
(!this.state.aSelected && !this.state.bSelected)
? false
: true
}
/>
</div>
);
}
}

ReactDOM.render(<App selectedValue='a'/>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

disabled checked checkbox is not being sent to model on form submit

On form submit you need to enable checkbox first

$(form).submit(){
$('.verification5').removeAttr('disabled');
}

Or instead of using disabled property you can also use readonly property as readOnly property can be sent to server on form submit.



Related Topics



Leave a reply



Submit