I am trying to limit a selection in my list - string check boxes. However, I can't seem to select the object in HTML. I am using an application builder with my objects and trying to implement an additional JavaScript code.
I have tried using the document.getElementsByClassName(), .querySelector() and .getElementsByTag() to select my objects but no success.
I can't figure out as to why this code won't work. Could it be because I am wasn't able to construct my code correctly or use the selectors properly? I am not really familiar with JavaScript, any help or directions would be very appreciated.
var checks = document.querySelectorAll(".item");
var max = 2;
for (var i = 0; i < checks.length; i++)
checks[i].onclick = selectiveCheck;
function selectiveCheck(event) {
var checkedChecks = document.querySelectorAll(".item:checked");
if (checkedChecks.length >= max + 1)
return false;
}
<div id="MultiSelectBox" class="ListData SelectBox cbFormSelect" style="z-
index: 1002; left: 109px; top: 43px; width: 107px; height: 151px;
background-color: rgb(255, 255, 255);">
<div class="Body" style="overflow-y: auto;">
<div style="display: table; width: 100%;">
<div style="">
<div class="Item" style="outline: none;">
<input type="checkbox" style="outline: none; margin-left: 0px; margin-
right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_0"><label for="InsertRecordCheckList_3a4dfdb26cc834_checked_0">Apple</label></div>
<div class="Item" style="outline: none;"><input type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_1"><label for="InsertRecordCheckList_3a4dfdb26cc834_checked_1">Banana</label></div>
<div class="Item" style="outline: none;"><input type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_2"><label for="InsertRecordCheckList_3a4dfdb26cc834_checked_2">Melon</label></div>
<div class="Item" style="outline: none;"><input type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_3"><label for="InsertRecordCheckList_3a4dfdb26cc834_checked_3">Mango</label></div>
<div class="Item" style="outline: none;"><input type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_4"><label for="InsertRecordCheckList_3a4dfdb26cc834_checked_4">Grapes</label></div>
<div class="Item" style="outline: none;"><input type="checkbox" style="outline: none; margin-left: 0px; margin-right: 6px;" id="InsertRecordCheckList_3a4dfdb26cc834_checked_5"><label for="InsertRecordCheckList_3a4dfdb26cc834_checked_5">Peach</label></div>
</div>
</div>
</div>
</div>
The expected result is quite simple, the script shouldn't not allow all users to select more than 2 options.
classof elements is set to"Item"at HTML though selector used at JavaScript is".item". Selectors strings are case sensitive. There are no elements havingclassset to"item"at HTML.selectiveCheckonly evaluaties the.lengthofcheckedChecks, correct? What is the expected result?