I want to disable all the elements inside Fieldset but enable few buttons inside it. Demo:
<fieldset ng-disabled="true"> <legend>Personalia:</legend> Name: <input type="text"><br> Email: <input type="text"><br> Date of birth: <input type="text"> <input type="button" value="See More (Enable this!!) " ng-click="ButtonClicked1()" ng-disabled="false"/> Somethingelse: <input type="text"> <input type="button" value="View Details " ng-click="ButtonClicked2()"/> </fieldset>
To disable a button using only JavaScript you need to set its disabled property to false . For example: element. disabled = true . And to enable a button we would do the opposite by setting the disabled JavaScript property to false .
A disabled fieldset is unusable and un-clickable. The disabled attribute can be set to keep a user from using the fields until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the fieldset usable again.
I have 2 solutions for you:
<legend>
label { display: block; }
<fieldset disabled> <legend> Personalia: <button>See more</button> </legend> <label>Name: <input></label> <label>Email: <input></label> <label>Date of birth: <input></label> <label>Somethingelse: <input></label> <button>View Details</button> </fieldset>
You can use aria-roles to “fake” a button role="button"
(see ARIA button role). Remember to add the necessary accessibility features to make this button clickable for users without display screens or mouse. The important attributes are role="button"
(for screen readers) and tabindex="0"
(for keyboard navigation), also remember to add a handler for keypress
for Enter and Space in case your user doesn’t have a mouse.
const disabledButton = document.querySelector('.disabled-button'); const disabledOutput = document.querySelector('.disabled-output'); const enabledButton = document.querySelector('.enabled-button'); const enabledOutput = document.querySelector('.enabled-output'); function increment(output) { const current = Number.parseInt(output.textContent); output.textContent = `${current + 1}`; } disabledButton.addEventListener('click', () => increment(disabledOutput)); disabledButton.addEventListener('keypress', event => { if (['Enter', ' '].includes(event.key)) { increment(disabledOutput); } }); enabledButton.addEventListener('click', () => increment(enabledOutput)); enabledButton.addEventListener('keypress', event => { if (['Enter', ' '].includes(event.key)) { increment(enabledOutput); } });
label { display: block; } [role="button"] { -webkit-appearance: button; appearance: button; cursor: default; font-style: normal; -webkit-user-select: none; user-select: none; }
<fieldset disabled> <legend>Disabled form elements</legend> <label>Input 1<input name="input 1"></label> <label>Input 2<input name="input 2"></label> <button class="disabled-button" name="disabled-button" > This is disabled </button> <i class="enabled-button" role="button" tabindex="0" > This is enabled </i> </fieldset> <label> Disabled button clicks: <output class="disabled-output">0</output> </label> <label> Enabled button clicks: <output class="enabled-output">0</output> </label>
Use icons instead of buttons and attaching the click
event to the icons. This bypasses the disabled fieldset and works like a charm.
<fieldset disabled='disabled'> <img src='/images/trash-can.png' ng-click='openModal()'/> </fieldset>
and the javascript (using angularjs)
$scope.openModal = ()=>{ // do stuff };
Bootstrap can style a span to look exactly like a button. Spans do not use, or inherit, the disabled
attribute.
<fieldset disabled='disabled'> <span class="btn btn-default" ng-click='openModal()'> Button Text </span> </fieldset>
and the javascript (using angularjs)
$scope.openModal = ()=>{ // do stuff };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With