How to select all checkboxes without changing var parameters - javascript

I using ViewModel and I have table with checkboxes, I can only check one by one boxes, but I need check all option but I need my var values to be defined, because I pass that values to controller and do something.
$('.checktip').click(function () {
var idemployee = $('.idemployee').val();
var idtip= $(this).attr('idtip');
var chk = $(this).prop('checked');
$.ajax({
url: UrlSettingsDocument.Books,
data: { idemployee: idemployee, idtip: idtip, chk: chk },
type: "POST",
success: function (result) {
if (result.result == "Redirect") {
window.location = result.url;
}
}
});
});
My .cshtml
<table class="table table-striped grid-table">
<tr>
<th>Samp</th>
<th>Id of Book</th>
<th>
//Button for check all *(NOT IMPLEMENTED)*
<button type="button" class="checkall">Select/Deselect</button>
</th>
</tr>
#foreach (var item in (IEnumerable<cit.Models.getCheIdTip_Result>)Model)
{
<tr>
<td>#item.idtip</td>
<td>#item.tipname</td>
<td>
<div class="pure-checkbox">
<input type="checkbox" idtip="#item.idtip" class="checktip" checked="#(item.idemployee == ViewBag.idemployee ? true : false)" name="#item.id.ToString()" id="#item.id.ToString()" />
<label for="#item.id.ToString()"></label>
</div>
</td>
</tr>
}
</table>
<input type="hidden" value="#ViewData["idemployee"]" name="idemployee" id="idemployee" class="idemployee" />
</div>

Use check box to check all checkbox instead of button.
On check change, using the class check/uncheck all checkbox inside the table.
$('#checkall').on('change', function() {
$('.checktip:checkbox').prop('checked', $(this).is(":checked"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-striped grid-table">
<tr>
<td><input type="checkbox" id="checkall" />Check All</td>
<td><input type="checkbox" class="checktip" /></td>
</td>
</td>
</td>
<td><input type="checkbox" class="checktip" /></td>
</td>
</td>
<td><input type="checkbox" class="checktip" /></td>
</td>
<td><input type="checkbox" class="checktip" /></td>
</tr>
</table>

Related

javastript get "input number" from class by checkbox

I have a table with 2 columns: checkbox and quantity.
<form action="{% url 'create-order' %}" method="POST">
{% csrf_token %}
<table class="table table-responsive table-borderless">
<thead>
<th> </th>
<th>Quantity</th>
</thead>
<tbody>
{% for item in items %}
<tr class="align-middle alert border-bottom">
<td>
<input type="checkbox" id="check" name="item" value="{{ item.id }}" onclick={changeQuantityState(this)}>
</td>
<td>
<input class="input" name="quantity" id="qnt" min="0" value=0 type="number" disabled>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="submitButton">
<button type="submit" class="lgbtn green">Go to order</button>
</div>
</form>
I have a handler for the checkbox: if checkbox is checked, then I can select the quantity. Otherwise, the quantity disabled.
function changeQuantityState(checkbox) {
checkbox.addEventListener('change', function(event) {
var quantity = document.getElementById('qnt');
if (event.currentTarget.checked) {
quantity.disabled = false;
quantity.value = 1;
}
else {
quantity.disabled = true;
quantity.value = 0;
}
});
}
I need change this line: var quantity = document.getElementById('qnt'); because I need the quantity for the current row in the table, for the current checkbox, but this code line always return first quantity
Your document.getElementById('qnt') will always return the first element that matches the query. You should always have only one element that has a specific ID. If the ID is present multiple times change that to something else, like a class. IDs must be unique.
Here's a quick example on how to use event.currentTarget to get the input you need. I've used parentElement.parentElement to get the <tr> element on which I use querySelector to select the input that's next to the checkbox.
const checkboxes = document.querySelectorAll('.checkbox');
const handleCheckboxChange = (ev) => {
const quantityInput = ev.currentTarget.parentElement.parentElement.querySelector('.quantity');
if (ev.target.checked) {
console.log('Would enable input', quantityInput);
// enable input
} else {
console.log('Would disable input: ', quantityInput);
// disable input
}
}
checkboxes.forEach((c) => c.addEventListener('change', handleCheckboxChange));
<table>
<thead>
<tr>
<th> </th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" class="checkbox" /></td>
<td><input placeholder="Quantity" class="quantity" /></td>
</tr>
<tr>
<td><input type="checkbox" class="checkbox" /></td>
<td><input placeholder="Quantity" class="quantity" /></td>
</tr>
<tr>
<td><input type="checkbox" class="checkbox" /></td>
<td><input placeholder="Quantity" class="quantity" /></td>
</tr>
</tbody>
</table>

How to find controls inside a class with specific class name

Hello all I have a table as follows
function checkAll2(rowClass, status) {
var dynamicClass = $('.' + rowClass);
// alert($('.1').find(":checkbox").length);
alert($('input:checkbox.1').length);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table1">
<tr>
<td>1a:</td>
<td><input type="checkbox" name="selected" value="1a" class="1"></td>
</tr>
<tr>
<td>1b:
</td>
<td><input type="checkbox" name="selected" value="1b" class="1"></td>
</tr>
</table>
Select All/None above<input type="checkbox" onclick="checkAll2(1,this.checked)" />
But what I need is I would like to reuse this for different classes too so I will pass rowClass so that my input:checkbox should be appended with rowClass and give me the count or the list of controls with that class
You should get the count of the .1 classes this way for it to be "dynamic"
function checkAll2(rowClass, status) {
var dynamicClass = $('.' + rowClass);
console.log($('input:checkbox.'+rowClass).length);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<table id="table1">
<tr>
<td>1a:</td>
<td><input type="checkbox" name="selected" value="1a" class="1"></td>
</tr>
<tr>
<td>1b:
</td>
<td><input type="checkbox" name="selected" value="1b" class="1"></td>
</tr>
</table>
Select All/None above<input type="checkbox" onclick="checkAll2(1,this.checked)" />

Get all the values of td columns from table where checkbox is checked

I have a table as below:
..
There have been multiple questions asked for getting the values but in this case I should always have a parent item name. Suppose If a user selected only one subitem in "Shirts", then I should be able to get all the values from the selected tr and with that I need parent item name also i.e "shirts" and if some one clicks on all the subitems of a parent item, then all the values of all tr are need to be in some sort of array object on click of a "Save" button. I am trying hard to do this. Any help would be really appreciated. Though I have attached the HTML but this HTML is being generated at run time.
HTML:
<table>
<tr>
<td> </td>
<td> </td>
<td>Name</td>
<td>Sub Item</td>
<td>User Input</td>
</tr>
<tr>
<td>
<input type="checkbox" id="chkGroup1" class="cls1" onclick="checkUncheckAll(this);" />
</td>
<td>Shirts
</td>
</tr>
<tr>
<td>
<input type="checkbox" class="cls1" name="Group1" onclick="CheckCorrespondingHeader(this);" /></td>
<td> </td>
<td>Item1</td>
<td>SubItem1</td>
<td>
<input id="1datepicker" name="1datepicker" type="text" /><script>
</script></td>
</tr>
<tr>
<td>
<input type="checkbox" class="cls1" name="Group1" onclick="CheckCorrespondingHeader(this);" /></td>
<td> </td>
<td>Item2</td>
<td>SubItem2</td>
<td>
<input id="2datepicker" name="2datepicker" type="text" /><script>
</script></td>
</tr>
<tr>
<td>
<input type="checkbox" class="cls1" name="Group1" onclick="CheckCorrespondingHeader(this);" /></td>
<td> </td>
<td>Item3</td>
<td>SubItem3</td>
<td>
<input id="3datepicker" name="3datepicker" type="text" /><script>
</script></td>
</tr>
<tr>
<td>
<input type="checkbox" id="chkGroup2" class="cls2" onclick="checkUncheckAll(this);" />
</td>
<td>Jeans
</td>
</tr>
<tr>
<td>
<input type="checkbox" class="cls2" name="Group2" onclick="CheckCorrespondingHeader(this);" /></td>
<td> </td>
<td>Item4</td>
<td>SubItem4</td>
<td>
<input id="4datepicker" name="4datepicker" type="text" /><script>
</script></td>
</tr>
<tr>
<td>
<input type="checkbox" class="cls2" name="Group2" onclick="CheckCorrespondingHeader(this);" /></td>
<td> </td>
<td>Item5</td>
<td>SubItem5</td>
<td>
<input id="5datepicker" name="5datepicker" type="text" /><script>
</script></td>
</tr>
<tr>
<td>
<input type="checkbox" class="cls2" name="Group2" onclick="CheckCorrespondingHeader(this);" /></td>
<td> </td>
<td>Item6</td>
<td>SubItem6</td>
<td>
<input id="6datepicker" name="6datepicker" type="text" /><script>
</script></td>
</tr>
</table>
Script code looks like below:
<script>
function checkUncheckAll(sender) {
var chkElements = document.getElementsByClassName(sender.className);
for (var i = 0; i < chkElements.length; i++) {
chkElements[i].checked = sender.checked;
}
}
function CheckCorrespondingHeader(sender) {
ControlLength = $("[name='" + sender.name + "']").length;
var countchecks = 0;
$("[name='" + sender.name + "']").each(function () {
if ($(this).prop('checked') == true) {
countchecks = countchecks + 1;
}
});
if (ControlLength == countchecks) {
$("#chk" + sender.name).attr('checked', 'checked');
}
else {
$("#chk" + sender.name).prop('checked', false);
}
}
function PickAllCheckedRows() {
}
</script>
As far as I can tell your code should work if you fix one issue. You are determining the number of sub rows that need to be checked to make the header row be checked using $("[name='" + sender.name + "']").length;. But unless I'm mistaken sender.name is never set. Of course if you set it this still won't work because your each function will include the header row. There are several solutions to this but I would recommend using a data attribute instead of the name attribute like so:
Markup:
<table>
<tr>
<!-- head -->
<td><input type="checkbox" data-head-for-group="Group1" ... /></td>
</tr>
<tr>
<!-- row 1 -->
<td><input type="checkbox" data-in-group="Group1" ... /></td>
</tr>
<tr>
<!-- row 2 -->
<td><input type="checkbox" data-in-group="Group1" ... /></td>
</tr>
<tr>
<!-- head -->
<td><input type="checkbox" data-head-for-group="Group2" ... /></td>
</tr>
<tr>
<!-- row 3 -->
<td><input type="checkbox" data-in-group="Group2" ... /></td>
</tr>
</table>
Script:
function CheckCorrespondingHeader(sender) {
var group = $("[data-in-group='" + sender.data('headForGroup') + "']");
var groupSize = group.length;
var countchecks = 0;
group.each(function () {
if ($(this).prop('checked') === true) {
countchecks = countchecks + 1;
}
});
if (groupSize === countchecks) {
$(sender).attr('checked', 'checked');
} else {
$(sender).prop('checked', false);
}
}

Getting textbox values inside table using jQuery

Html Table
<table id="tb">
<tr>
<th>First Name</th>
<th>Last Name </th>
<th>Coutry</th>
</tr>
<tr>
<td><input type="text" id="FName"/> </td>
<td><input type="text" id="LName"/> </td>
<td><input type="text" id="Country"/> </td>
</tr>
<tr>
<td><input type="text" id="FName"/> </td>
<td><input type="text" id="LName"/> </td>
<td><input type="text" id="Country"/> </td>
</tr>
</table>
Jquery
//Here I am looping through all the rows in table
$('#tb tr').each(function(i, row) {
//Here I need to loop the tr again ( i.e. row)
$(row, "input").each(function(i, sr) {
//here I want to get all the textbox values
alert($(sr).eq(0).val());
alert($(sr).eq(1).val());
alert($(sr).eq(2).val());
});
});
But I am getting undefined values.
I want to get all the values from text boxes which are inside the table.
Please help me resolve the issue.
You can use
$('#tb tr').each(function(i, row) {
$(this).find('input').each(function() {
alert($(this).val())
})
});
Working Demo
$('#tb tr').each(function(i, row) {
$(this).find('input').each(function() {
alert($(this).val())
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tb">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Coutry</th>
</tr>
<tr>
<td>
<input type="text" id="FName" value="1" />
</td>
<td>
<input type="text" id="LName" value="2" />
</td>
<td>
<input type="text" id="Country" value="3" />
</td>
</tr>
<tr>
<td>
<input type="text" id="FName" value="4" />
</td>
<td>
<input type="text" id="LName" value="5" />
</td>
<td>
<input type="text" id="Country" value="6" />
</td>
</tr>
</table>
The line has the context switched with the selector
$(row, "input").each(function (i, sr) {
It should be
$("input", row).each(function (i, sr) {
or
$(row).find("input").each(function (i, sr) {
You can just use :
$('#tb tr input[type=text]').each(function(){
alert($(this).val());
});

Jquery isn't setting radio button

I'm trying to have "set all" radio buttons at the bottom of my popup control so when the user has a long list of conflicts to resolve, they can just select one of the radio buttons and the option will be quickly select. However, my javascript fires, and seems to find the radio button, but fails to actually set the radio button.
I have a gridview gvErrors that is being looped though and in the second cell of each gridview row is a table with the options (tblOptions). I have tried using .attr("checked", true), .setAttribute("checked", true), and .prop("checked", true). I am receiving no errors, in the console, but the radio buttons all remain unchecked. Any help with this would be appreciated. Below is the Javascript.
<script type="text/javascript" language="javascript">
function selectAll(option) {
var grid = document.getElementById("<%=gvErrors.ClientID%>");
for (var i = 0; i < grid.rows.length; i++)
{
var row = grid.rows[i];
var table = $(row).find("tblOptions");
var radio = $(table).find("input[name*=" + option + "]:radio");
//$('td input:radiobutton', '#tblOptions').prop('checked', true);
$(radio).prop("checked", "checked");
var test = "";
}
};
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function () {
//This handles the rows or colums selection
$("#<%=rdbCancelAll.ClientID%>").click(function() {
selectAll("rdbCancel");
});
});
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function () {
//This handles the rows or colums selection
$("#<%=rdbReplaceAll.ClientID%>").click(function() {
selectAll("rdbReplace");
});
});
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function () {
//This handles the rows or colums selection
$("#<%=rdbRenameAll.ClientID%>").click(function() {
selectAll("rdbRename");
});
});
</script>
Small example of the gridview:
<table class="tableinfo nocollapse c6" cellspacing="1" cellpadding="2" border="0" id="ctl00_Main_gvErrors">
<tbody>
<tr class="tableinfobody tableinfoGray">
<th scope="col"><span class="c1">Current Name</span></th>
<th scope="col"><span class="c1">Options</span></th>
<th scope="col">Differences</th>
</tr>
<tr class="tableinfobody">
<td class="l"><span id="ctl00_Main_gvErrors_ctl02_lblName">Test1</span></td>
<td class="l">
<table id="ctl00_Main_gvErrors_ctl02_tblOptions" border="0">
<tbody>
<tr>
<td><input id="ctl00_Main_gvErrors_ctl02_rdbCancel" type="radio" name=
"ctl00$Main$gvErrors$ctl02$Options" value="rdbCancel" /><label for=
"ctl00_Main_gvErrors_ctl02_rdbCancel">Cancel adding signal.</label></td>
</tr>
<tr>
<td><input id="ctl00_Main_gvErrors_ctl02_rdbReplace" type="radio" name=
"ctl00$Main$gvErrors$ctl02$Options" value="rdbReplace" /><label for=
"ctl00_Main_gvErrors_ctl02_rdbReplace">Replace curent signal with
imported signal.</label></td>
</tr>
<tr>
<td><input id="ctl00_Main_gvErrors_ctl02_rdbRename" type="radio" name=
"ctl00$Main$gvErrors$ctl02$Options" value="rdbRename" /><label for=
"ctl00_Main_gvErrors_ctl02_rdbRename">Rename imported signal to:</label>
<input name="ctl00$Main$gvErrors$ctl02$txtNewName" type="text" value=
"Test1_1" id="ctl00_Main_gvErrors_ctl02_txtNewName" class="c2" /></td>
</tr>
</tbody>
</table>
</td>
<td class="l">
<input type="hidden" name="ctl00$Main$gvErrors$ctl02$hfParamInternalUnmatched"
id="ctl00_Main_gvErrors_ctl02_hfParamInternalUnmatched" value=
"EBC1-Test1" /> <input type="hidden" name=
"ctl00$Main$gvErrors$ctl02$hfParamInternalMatched" id=
"ctl00_Main_gvErrors_ctl02_hfParamInternalMatched" value="Test1" />
<table class="tableinfo c5" cellspacing="1" cellpadding="2" border="0">
<tbody>
<tr class="tableinfobody tableinfoGray">
<th>Value Name</th>
<th>Current</th>
<th>Imported</th>
</tr>
<tr class="tableinfobody">
<td class="c3">Unit</td>
<td class="c4"></td>
<td class="c4">flag</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr class="tableinfobody tableinfoGray">
<td class="l"><span id="ctl00_Main_gvErrors_ctl03_lblName">Test2</span></td>
<td class="l">
<table id="ctl00_Main_gvErrors_ctl03_tblOptions" border="0">
<tbody>
<tr>
<td><input id="ctl00_Main_gvErrors_ctl03_rdbCancel" type="radio" name=
"ctl00$Main$gvErrors$ctl03$Options" value="rdbCancel" /><label for=
"ctl00_Main_gvErrors_ctl03_rdbCancel">Cancel adding signal.</label></td>
</tr>
<tr>
<td><input id="ctl00_Main_gvErrors_ctl03_rdbReplace" type="radio" name=
"ctl00$Main$gvErrors$ctl03$Options" value="rdbReplace" /><label for=
"ctl00_Main_gvErrors_ctl03_rdbReplace">Replace curent signal with
imported signal.</label></td>
</tr>
<tr>
<td><input id="ctl00_Main_gvErrors_ctl03_rdbRename" type="radio" name=
"ctl00$Main$gvErrors$ctl03$Options" value="rdbRename" /><label for=
"ctl00_Main_gvErrors_ctl03_rdbRename">Rename imported signal to:</label>
<input name="ctl00$Main$gvErrors$ctl03$txtNewName" type="text" value=
"Test2_1" id="ctl00_Main_gvErrors_ctl03_txtNewName" class="c2" /></td>
</tr>
</tbody>
</table>
</td>
<td class="l">
<input type="hidden" name="ctl00$Main$gvErrors$ctl03$hfParamInternalUnmatched"
id="ctl00_Main_gvErrors_ctl03_hfParamInternalUnmatched" value=
"HCMData3-Testw" /> <input type="hidden" name=
"ctl00$Main$gvErrors$ctl03$hfParamInternalMatched" id=
"ctl00_Main_gvErrors_ctl03_hfParamInternalMatched" value=
"PrimaryData3-Testw" />
<table class="tableinfo c5" cellspacing="1" cellpadding="2" border="0">
<tbody>
<tr class="tableinfobody tableinfoGray">
<th>Value Name</th>
<th>Current</th>
<th>Imported</th>
</tr>
<tr class="tableinfobody">
<td class="c3">SA</td>
<td class="c4">3, 239</td>
<td class="c4">239</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr class="tableinfobody tableinfoBlue">
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
Any help clearing this up would be greatly appreciated.
Use the value as a selector like so
function selectAll(option) {
var radio = $("input[value=" + option + "]");
$(radio).prop("checked", "checked");
}
$('input[type="button"]').on('click', function(){
var value = $(this).data('attr');
selectAll(value);
});
http://jsfiddle.net/3u3z4bLn/3/

Categories

Resources