Добавить js/DOM/checkbox.md
This commit is contained in:
27
js/DOM/checkbox.md
Normal file
27
js/DOM/checkbox.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# Обработка input checkbox
|
||||
|
||||
|
||||
#### Получить массив выбранных чекбоксов
|
||||
|
||||
|
||||
|
||||
```js
|
||||
/**
|
||||
* @params str parent_id - ID элемента откуда начинается поиск input с типом checkbox
|
||||
*
|
||||
* @result arr - Массив выбранных checkbox`ов
|
||||
*
|
||||
*/
|
||||
function getCheckedCheckBoxes(parent_id) {
|
||||
|
||||
if(parent_id == "") alert("Не указан ID родителя");
|
||||
var checkboxes = document.getElementById(parent_id).querySelectorAll("input[type='checkbox']");
|
||||
var checkboxesChecked = [];
|
||||
for (var index = 0; index < checkboxes.length; index++) {
|
||||
if (checkboxes[index].checked) {
|
||||
checkboxesChecked.push(checkboxes[index].value);
|
||||
}
|
||||
}
|
||||
return checkboxesChecked; // для использования в нужном месте
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user