I have the following code in the JSFiddle and I’m facing two issues:
Issue #1:
If I click anywhere outside Go Right, Employee Decision etc checkbox, it is still getting checked.Why it is happening like this? I was expecting it to get checked only when user clicks on the checkbox.
Issue #2:
I’m trying to select Tag1 when Late Payment is selected from the Delay Reason for All Employees dropdown using jQuery but that doesn’t seem to be working.
I assume you mean if you click on the words “Go Right”, “Turn Off the Switch”, etc or the white space to the right of those words.
That’s because you’ve wrapped the checkbox in the Label, which is taking up the whole box left-right. Clicking on a Label assigned to a click-sensitive input will trigger the input.
call $("#reasontagSelect").trigger("change"); after you update the value, so that Javascript redraws the box and notices that the value’s changed. Your code is setting the value, its just not triggering a redraw.
I only want the checkbox to be checked when the user clicks on the checkbox and not when the whitespace is clicked or the behavior that’s happening now.
The label text should also trigger the change for better accessibility. Just change the display:block on the labels to display:inline-block. That will stop the white space to the right of the text being clickable.
It looks like you have them all wrapped in divs anyway so that shouldn’t trigger a change in layout although I haven’t tested as js fiddle doesn’t work on a mobile.
Alternatively I guess you could just set width:fit-content to the label but again I haven’t tested that.
First off looking at your html you have duplicate ids of id='topList' — ids need to be unique
A bit of a re-work of your HTML. Putting the label after the checkbox seems to do the trick. Note I have added the for attribute to connect each label to it’s associated input.
JS
I don’t do much with jQuery, but this is how I would do it, keeping the JS separate to the HTML
function reasonChanged(event) {
// on an event e.g. an onchange, an event object is passed to the handler
// the item that the event occurred on e.g. #reasonSelect, is the event.target
const input = event.target
if (input.value === 'Late Payment') {
$("#reasonTagSelect")
.val("tag01")
.trigger("change");
}
}
// add an onchange listener to #reasonSelect passing in the handler function.
$('#reasonSelect').change(reasonChanged);
Edit:
You could have the handler reset to a default ‘no selection’ option, if the value is not ‘Late Payment’
function reasonChanged(event) {
// on an event e.g. an onchange, an event object is passed to the handler
// the item that the event occurred on e.g. #reasonSelect, is the event.target
const input = event.target;
const tagSelect = $("#reasonTagSelect");
if (input.value === 'Late Payment') {
tagSelect.val("tag01");
} else {
// reset to default "-No Selection-"
tagSelect.val("");
}
tagSelect.trigger("change");
}
// add an onchange listener to #reasonSelect passing in the handler function.
$('#reasonSelect').change(reasonChanged);