I've 2 columns with checkboxes when one column is checked all respective are checked likewise in 2nd column but the problem is here, client wants when One column of checkbox is checked then 2nd column will be disable or throw alert message to check only one column at a time?
function SelectAll1(headerchk, gridId) {
var gvcheck = document.getElementById(gridId);
var i, j;
if (headerchk.checked) {
for (i = 0; i < gvcheck.rows.length - 1; i++) {
var inputs = gvcheck.rows[i].getElementsByTagName('input');
for (j = 1; j < inputs.length; j++) {
if (inputs[j].type == "checkbox") {
inputs[j].checked = true;
}
}
}
}
else {
for (i = 0; i < gvcheck.rows.length - 1; i++) {
var inputs = gvcheck.rows[i].getElementsByTagName('input');
for (j = 1; j < inputs.length; j++) {
if (inputs[j].type == "checkbox") {
inputs[j].checked = false;
}
}
}
}
}
You can ch
I don't really know what you're trying to do without the HTML but here's a start you can build off.
var selected = $('.check:checked').length;
if (selected >= 1) {
$('.check').prop('disabled', true);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="check" checked="checked">
<input type="checkbox" class="check">
<input type="checkbox" class="check">
From the little info given I assume this:
SelectAll1 clear or set all checkboxes in column 1.
SelectAll2 clear or set all checkboxes in column 2.
headerchk is a checkbox when clicked clears or sets all.
Do this change:
After each line in the code where SelectAll1(headerchk, gridId) is called you change it to SelectAll1(headerchk.checked, gridId).
Insert another line below this line with a negation:
SelectAll2(!headerchk.checked, gridId)
After each line in the code where SelectAll2(headerchk, gridId) is called you change it to SelectAll2(headerchk.checked, gridId).
Insert another line below this line with a negation:
SelectAll1(!headerchk.checked, gridId)
You only showed me the code for SelectAll1, so I assume SelectAll2 is the same when you not yet know how to make them different. Correct me if this is not the fact!
Continue by change the function SelectAll1 to the following:
function SelectAll1(header_checked, gridId) {
var gvcheck = document.getElementById(gridId);
var i, j;
for (i = 0; i < gvcheck.rows.length - 1; i++) {
var inputs = gvcheck.rows[i].getElementsByTagName('input');
for (j = 1; j < inputs.length; j++) {
if (inputs[j].type == "checkbox") {
inputs[j].checked = header_checked;
}
}
}
}
The else-part and if is eliminated by moving the condition to the assignment that is eiher true or false. The .checked part is moved outside the function.
Do the same change in SelectAll2
Test if it works and report to me!
Related
I am using the following code to uncheck selected check boxes on page refresh.
for (i = 0; i < object.length; i++)
{
object[i].checked = false;
}
The validation after refresh will work but the checkboxes will stay as selected.(Tick wont go)
Please Help.
The attribute checked of the checkbox didn't use value true or false. (Input checked attribute on W3Schools)
In order to uncheck the checkboxes, you have to remove the attribute from the element:
for (i = 0; i < object.length; i++) {
if (object[i].hasAttribute("checked")) {
object[i].removeAttribute("checked");
}
}
Obviously, you can check the checkboxes by adding the attribute to the element:
for (i = 0; i < object.length; i++) {
if (!object[i].hasAttribute("checked")) {
object[i].addAttribute("checked");
}
}
This will work on every single browser by the way.
try :
for (i = 0; i < object.length; i++)
{
object[i].checked = "off";
}
I am adding a radio button in a gridrow dynamically on rowdatabound event.
I want value of selected radio button of specific row of grid.
Currently I am getting value bt it will no return the value for specific row index.
See my below code:
function GetBlastid()
{
var gv = document.getElementById("<%=grdOofMailProcess.ClientID%>");
var rbs = gv.getElementsByTagName("input");
var flag = 0;
for (var i = 0; i < rbs.length; i++)
{
if (rbs[i].type == "radio")
{
if (rbs[i].checked)
{
alert(rbs[i]);
flag = 1;
document.getElementById('hdnBlastId').value=rbs[i].value
break;
}
}
}
}
well once you get which radiobox is checked navigate to its parent and get the row index row.rowIndex , i couldnt give you the exact code coz i dont know how you placed the radio button, whether its directly under a column or have a different parent.
function GetBlastid()
{
var gv = document.getElementById("<%=grdOofMailProcess.ClientID%>");
var rbs = gv.getElementsByTagName("input");
var flag = 0;
for (var i = 0; i < rbs.length; i++)
{
if (rbs[i].type == "radio")
{
if (rbs[i].checked)
{
alert(rbs[i]);
//here get the parent of rbs[i] and navigate to the row to get the row index.
flag = 1;
document.getElementById('hdnBlastId').value=rbs[i].value
break;
}
}
}
}
I've got a javascript function
<head>
<title>
Test
</title>
<script type="text/javascript">
function GetResult()
{
count = 0;
for(var i=0;i<10;i++){
for(var j=1;j<4;j++){
if (document.getElementById("label"+i+j).checked){
count +=1;
}
}
}
if (count!=10)
alert("Please answer all the questions");
else alert(count);
}
</script>
In the code there are a lot of radiobutton. ther look like
<input type="radio" name="q1" value="1" id="label01"/>
But my javascript function never shows alert.
The button that is supposed to call function is
<input type="button" value="Result" onclick="GetResult()"/>
Maybe button doesn't call GetResult?
To elaborate what Felix already said: Here is how you can check whether or not document.getElementById found the specified element (it will return null if it failed).
for (var i = 0; i < 10; i++) {
for (var j = 1; j < 4; j++) {
// Store the result in a local variable
var label = document.getElementById("label"+i+j);
// Include a check whether "null" got returned
if (label && label.checked) {
count +=1;
}
}
}
Try this:
function GetResult() {
var count = 0;
for (var i = 0; i < 10; i++){
for (var j = 1; j < 4; j++){
var label = document.getElementById("label" + i + j);
if (label && label.checked) {
count +=1;
}
}
}
if (count != 10) {
alert("Не все отвечено");
} else {
alert(count);
}
}
Added var to the count declaration.
Fixed up some general formatting.
The important bit: checked to see if document.getElementById() returned a value before checking that value's checked property.
I have a Checkbox list. On the load of a page i want my first checkbox true and others are disable.I need to check only one checkbox from checkbox list and others should be disable.If use unchecked the checked checkbox then others should be enable means allowed only one check box checked.
My javascript code is here but on the load of page its not checked the first checkbox, also i want the id of each checkbox while using checkboxlist.
function CheckOptions(CheckBoxList) {
var checkboxlist = document.getElementById('CheckBoxList1');
var checkedCount = 0;
var options = CheckBoxList.getElementsByTagName('input');
for (var i = 0; i < options.length; i++) {
if (options[i].checked) {
checkedCount += 1;
}
}
if (checkedCount > 0) {
for (var j = 0; j < options.length; j++) {
if (!options[j].checked)
options[j].disabled = true;
} }
else {
for (var k = 0; k < options.length; k++) {
options[k].disabled = false;
}
}
}
If you're only wanting one checked at a time, it sounds like a group of radio buttons would better serve your purposes.
$(function() {
var checkboxes = $("input[type=checkbox]");
// select the first one on load
checkboxes.eq(0).attr("checked", "checked");
checkboxes.each(function(i, e) {
if (i > 0) {
$(e).attr("disabled", "disabled");
}
})
// handle further selections:
checkboxes.click(function() {
if ($(this).attr("checked")) {
var t = this;
checkboxes.each(function(i, e) {
if (e != t) {
$(e).attr("disabled", "disabled");
}
});
} else {
checkboxes.attr("disabled", null)
}
});
});
I am trying to validate the checkbox which is one of the items in ListView control.
I have the button to check so I define the ClientClick function on the button and wrote JavaScript code.
But It didn't work. Display the lstViewtest object null.
function btnclick() {
var listview = document.getElementById('<%=
lstviewtest.FindControl("tableItem").ClientID %>');
for (var i = 0; i < listview.rows.length; i++) {
var inputs = listview.rows[i].getElementsByTagName('input');
for (var j = 0; j < inputs.length; j++) {
if (inputs[j].type === "checkbox" || inputs[j].type === "checkboxsend")
if (inputs[j].checked)
return true;
}
alert("Please select at least one");
return false;
}
}