Ad
The Checkbox Check If It Is Checked Does Not Work
I have a problem seeing if the checkbox has been checked.
In particular, I have a table with id, name and country and an additional column with a checkbox. The purpose is to return all the ids of the checkbox that has been selected. But what I notice is that the if statement doesn't seem to work.
Can you kindly help me?
function GetSelected() {
var table = document.getElementById(mytable);
var rowCount = table.rows.length;
console.log(rowCount)
for (var i = 1; i < rowCount; i++) {
var row = table.rows[i];
var chkbox = table.getElementsByTagName("INPUT");
if (chkbox[i].checked) {
var row = checkBoxes[i].parentNode.parentNode;
id_art += row.cells[0].innerHTML;
console.log(id_art);
}
}
}
<table cellspacing="0" rules="all" border="1" id="my-table" style="border-collapse: collapse;">
<tr>
<th> </th>
<th style="width:80px">Check</th>
<th style="width:80px">Id</th>
<th style="width:120px">Name</th>
<th style="width:120px">Country</th>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>1</td>
<td>John Hammond</td>
<td>United States</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>2</td>
<td>Mudassar Khan</td>
<td>India</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>3</td>
<td>Suzanne Mathews</td>
<td>France</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>4</td>
<td>Robert Schidner</td>
<td>Russia</td>
</tr>
</table>
<br />
<input type="button" value="Get Id Selected" onclick="GetSelected()" />
Ad
Answer
You can grab all the checked checkboxes and loop over them to get the text in the next td element.
function getSelected() {
var cbs = document.querySelectorAll('#my-table input[type="checkbox"]:checked');
console.log(cbs.length);
const ids = Array.from(cbs).map(cb => cb.closest('td').nextElementSibling.textContent);
console.log(ids);
}
<table cellspacing="0" rules="all" border="1" id="my-table" style="border-collapse: collapse;">
<tr>
<th> </th>
<th style="width:80px">Check</th>
<th style="width:80px">Id</th>
<th style="width:120px">Name</th>
<th style="width:120px">Country</th>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>1</td>
<td>John Hammond</td>
<td>United States</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>2</td>
<td>Mudassar Khan</td>
<td>India</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>3</td>
<td>Suzanne Mathews</td>
<td>France</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>4</td>
<td>Robert Schidner</td>
<td>Russia</td>
</tr>
</table>
<br />
<input type="button" value="Get Id Selected" onclick="getSelected()" />
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad