Javascript & ASP.NET - Manage CheckBoxList -
i have checkboxlist dinamically filled db.
<asp:checkboxlist id="chklist1" runat="server" onclick="chklist1_onclick()" /> oce has been filled have several options , 1 of them has text "no response".
what want javascript function following:
1) if check "no response" other options must unchecked.
2) if check @ least 1 of options not "no response", "no response" option must unchecked.
hope clear. in advance.
my attempt was:
function chklist1_onclick() { var chklist1 = document.getelementbyid('<%= chklist1.clientid %>'); var chklist = chklist1.getelementsbytagname("input"); (var = 0; < chklist.length; i++) { if (chklist[i].checked && chklist[i].value == "6") { (var = 0; < chklist.length; i++) { if (chklist[i].checked && chklist[i].value != "6") { chklist[i].checked = false; } } } } } where 6 value of "no response" item. way resolve case 1)
assuming no response checkbox has value="" try following script:
<script type="text/javascript"> window.onload = function () { var noresponsecheckboxfilter = function(item) { return item.value == ''; }; var othercheckboxesfilter = function(item) { return !noresponsecheckboxfilter(item); }; var childinputs = document.getelementbyid('<%= chklist1.clientid %>').getelementsbytagname('input'); var checkboxes = array.prototype.slice.call(childinputs, 0).filter(function (item) { return item.type == 'checkbox'; }); (var = 0; < checkboxes.length; i++) { checkboxes[i].onclick = function () { if (this.value == '') { if (this.checked) { // uncheck other checkboxes var othercheckboxes = checkboxes.filter(othercheckboxesfilter); (var j = 0; j < othercheckboxes.length; j++) { othercheckboxes[j].checked = false; } } } else { // uncheck no response checkbox checkboxes.filter(noresponsecheckboxfilter)[0].checked = false; } }; } }; </script> if no response checkbox has different value empty string adapt tests in previous example.
Comments
Post a Comment