javascript select all checkboxes in a table
I want to make a page that has a table of various webpages with checkboxes next to each. I want the user to be able to select multiple sites then search the sites using a google bar. I have a table where each cell has a form filled with checkboxes. each cell has a checkall button that checks all the options in that cell. I would like to add a checkbox to select all the options on the page开发者_开发技巧. (yes I could just leave this option out but I kind of want to know how to access all the boxes in the cells anyway so that I can search with google like I want.) here is basically what I have. Its the section inside checkPage function that needs help at this point
<html>
<head>
<script type="text/javascript">
function checkAll(checkname, bx) {
for (i = 0; i < checkname.length; i++){
checkname[i].checked = bx.checked? true:false;
}
}
function checkPage(bx){
var bxs = document.getElementByTagName ( "table" ).getElementsByTagName ( "link" );
for(i = 0; i < bxs.length; i++){
bxs[i].checked = bx.checked? true:false;
}
}
</script>
</head>
<body>
<input type="checkbox" name="pageCheck" value="yes" onClick="checkPage(this)"><b>Check Page</b>
<table border="1" name ="table">
<tr>
<td name ="list00">
<form name ="list00">
<input type="checkbox" name="Check_ctr" value="yes" onClick="checkAll(document.list00.link, this)"><b>Check All</b><dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
</form>
</td>
<td><form name ="list01">
<input type="checkbox" name="Check_ctr" value="yes" onClick="checkAll(document.list01.link, this)"><b>Check All</b><dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
</form></td>
</tr>
<tr>
<td><form name ="list10">
<input type="checkbox" name="Check_ctr" value="yes" onClick="checkAll(document.list10.link, this)"><b>Check All</b><dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
</form></td>
<td><form name ="list11">
<input type="checkbox" name="Check_ctr" value="yes" onClick="checkAll(document.list11.link, this)"><b>Check All</b><dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
</form></td>
</tr>
</table>
</body>
</html>
function checkAll(bx) {
var cbs = document.getElementsByTagName('input');
for(var i=0; i < cbs.length; i++) {
if(cbs[i].type == 'checkbox') {
cbs[i].checked = bx.checked;
}
}
}
Have that function be called from the onclick attribute of your checkbox to check all
<input type="checkbox" onclick="checkAll(this)">
Edit I misread your question a little, i see you have attempted it in your code. the getElementsByTagName has to be plural which you may have typo'd there and has to be a tag as specified by the answer above
Edit: Passing the master checkbox as a parameter would allow for toggling check/uncheck as suggested by vol7ron and has been modified in this answer appropriately.
The question asks for all checkboxes on the page so this would suffice.
However, providing control of which elements to look for checkboxes can be achieved in may ways, too many to go into detail but examples could be document.getElementById(id).getElementsByTagName if all checkboxes to be controlled are branched nodes from one element.
Otherwise, you can iterate through a further tag name retrieval / custom class name retrieval to name a few.
Example: http://jsfiddle.net/vol7ron/kMBcW/
function checkPage(bx){
for (var tbls=document.getElementsByTagName("table"), i=tbls.length; i--; )
for (var bxs=tbls[i].getElementsByTagName("input"), j=bxs.length; j--; )
if (bxs[j].type=="checkbox")
bxs[j].checked = bx.checked;
}
Have you tried jQuery? It's becoming javascript standart library for DOM manipulation (stackoverflow using in too).
With it you could do $(':checkbox').prop('checked', true); to check every checkbox on the page(but you probably wan't to check then only in table).
Well anyway start using jQuery, it will make your life easier and happier, and will save you a lot of time.
using vanilla js
checkAll = box => {
const checkboxes = document.getElementsByTagName('input')
for (const checkbox of checkboxes) {
if (checkbox.type == 'checkbox')
checkbox.checked = box.checked
}
put in your html
<input type="checkbox" onclick="checkAll(this)">
tag name is the bit that starts the html tag eg <input
so the .getElementsByTagName ( "link" )
should be .getElementsByTagName ( "input" )
if you only want the name='link'
then if(bxs.name =="link") { ... change the checked }
... or even simpler if you want to flip all checkboxes in the corresponding form:
function checkAll(bx){
var form = bx.form;
var ischecked = bx.checked;
for (var i = 0; i < form.length; ++i) {
if (form[i].type == 'checkbox') {
form[i].checked = ischecked;
}
}
}
...
<input type="checkbox" onclick="checkAll(this)">
In my case, it helped me
<template>
<div class="container">
<ul class="list-group pt-1">
<li class="list-group-item align-center">
<button data-bs-toggle="collapse" data-bs-target="#dat_input" aria-expanded="false" aria-controls="income-expense" class="btn btn-sm border border-dark">
Yana
</button>
<nuxt-link :to="`/admin/question/excel/create`" class="btn btn-success btn-sm">Excel orqali test qo'shish</nuxt-link>
<span class="float-end">
<button type="button" accesskey="1" class="btn btn-green btn-sm ">1 kunlik</button>
<nuxt-link :to="`/admin/question`" class="btn btn-danger btn-sm " accesskey="b">Orqaga</nuxt-link>
</span>
</li>
<li id="dat_input" class="list-group-item collapse">
<p>Bu yerga endi biror nima qo`shiladi</p>
</li>
</ul>
<div class="card mt-1">
<table class="table table-sm">
<thead class="table-dark table-sm">
<tr>
<th scope="col" class="text-center">
<input class="form-check-input" type="checkbox" @click="selectAll(this, $event)">
</th>
<th scope="col" class="text-center"><i class="bi bi-sort-numeric-down-alt"></i></th>
<th scope="col" class="text-center"><i class="bi bi-calendar3"></i></th>
<th scope="col" class="float-left">Savol</th>
<th scope="col" class="float-left">To'g'ri javob</th>
<th scope="col" class="text-center"><i class="bi bi-alarm"></i></th>
<th scope="col" class="text-center"><i class="bi bi-cash"></i></th>
<th scope="col" class="text-center"><i class="bi bi-person-plus"></i></th>
<th scope="col" style="width: 7%; text-align: center;"><i class="bi bi-diagram-3"></i></th>
</tr>
</thead>
<tbody>
<tr v-for="(question, id) in questions">
<td width="1%" class="text-center align-middle">
<input class="form-check-input" type="checkbox" @click="checkbox($event, question.id)">
</td>
<td width="3%" class="text-center align-middle" v-text="question.id"></td>
<td width="8%" class="text-center align-middle" v-text="question.create"></td>
<td class="float-left align-middle">{{ question.question }} <i class="bi bi-arrow-right"></i></td>
<td class="float-left align-middle" v-text="question.answer[0]"></td>
<td width="1%" class="text-center align-middle" v-text="question.time"></td>
<td width="1%" class="text-center align-middle"> {{ question.money }}</td>
<td width="1%" class="text-center align-middle"> {{ question.rating }}</td>
<td width="8%" class="text-center align-middle">
<nuxt-link :to="`/admin/question/view/${question.id}`" class="btn btn-success btn-sm padingkichkina"><i class="bi bi-eye"></i></nuxt-link>
<nuxt-link :to="`/admin/question/edit/${question.id}`" class="btn btn-primary btn-sm padingkichkina"><i class="bi bi-pencil"></i></nuxt-link>
<a class="btn btn-outline-danger align-middle btn-sm padingkichkina" @click="deleteQuestion(question.id, question.question)"><i class="bi bi-trash"></i></a>
</td>
</tr>
</tbody>
</table>
</div>
<div class="card" v-if="next_page_url !== null">
<button class="btn btn-success" @click="getPaginateTrade">
Yana yuklash
</button>
</div>
</div>
</template>
<script>
import Swal from 'sweetalert2'
export default {
middleware: 'isAdmin',
data() {
return {
questions: [],
success: false,
next_page_url: null,
selected: [],
isCheckAll: false,
}
},
mounted() {
this.$loading.show()
this.getQuestions()
},
methods: {
async getQuestions() {
this.$axios.get('auth/questionsexcel')
.then((res) => {
this.questions = res.data.data.data;
if (res.data.next_page_url !== null) {
this.next_page_url = res.data.data.next_page_url
}
}).catch((error) => {
this.has_error = true
console.log(error.response.data.message)
});
this.$loading.hide();
},
async getPaginateTrade() {
this.$axios.get(`${this.next_page_url}`)
.then((res) => {
if (res.data.next_page_url !== null) {
this.next_page_url = res.data.data.next_page_url
const questions = this.questions;
this.questions = questions.concat(res.data.data.data)
} else {
this.SwalMixin("Boshqa savollar qolmadi", 'error')
}
}).catch((error) => {
this.has_error = true
console.log(error.response.data.message)
});
},
checkbox(event, id) {
this.isCheckAll = !this.isCheckAll;
let array = this.selected;
if (event.target.checked) {
this.selected = array.concat(id);
} else {
this.selected = array.filter(item => item !== id);
}
},
selectAll(bx, event) {
var cbs = document.getElementsByTagName('input'), i = 0;
if (event.target.checked) {
this.selected = this.questions.map(item => item.id);
for (i; i < cbs.length; i++) {
if (cbs[i].type == 'checkbox') {
cbs[i].checked = true;
}
}
} else {
this.selected = [];
for (i; i < cbs.length; i++) {
if (cbs[i].type == 'checkbox') {
cbs[i].checked = false;
}
}
}
},
deleteQuestion(id, name) {
Swal.fire({
title: "O'chirasizmi?",
html: `<b>${name}</b> ni o'chirmoqchimisiz?`,
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: "Ha o'chiraman",
cancelButtonText: "Yo'q hozir emas"
}).then((result) => {
if (result.isConfirmed) {
this.$axios.post('auth/deleteQuestion', {
id: id
}).then((response) => {
this.SwalMixin(response.data.data.message)
this.getQuestions()
}).catch((error) => {
this.SwalMixin(error.response.data.message, 'error')
});
}
})
},
}
}
</script>
精彩评论