Просмотр полной версии : Checkbox и ссылка
vasyakrn
05.07.2015, 01:35
Помогите написать js код чтоб при клике на ссылку отмечался чекбокс. Брать все в тег <label> не вариант, так как не работает в некоторих браузерах. Вот мой код
<input id="one" type="checkbox" />
<a id="one" >Один</a>
<input id="two" type="checkbox" />
<a id="two" >Два</a>
<input id="three" type="checkbox" />
<a id="three" >Три</a>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div class="block">
<input id="one" type="checkbox" />
<a href="#" id="one">Один</a>
<input id="two" type="checkbox" />
<a href="#" id="two">Два</a>
<input id="three" type="checkbox" />
<a href="#" id="three">Три</a>
</div>
<script>
document.querySelector('.block').onclick = function(e) {
e.preventDefault();
var target, id, elem;
target = e.target;
if(target.nodeName != 'A') return;
id = target.getAttribute('id');
if(!id) return;
elem = document.getElementById(id);
elem.checked = (elem.checked) ? false : true;
};
</script>
</body>
</html>
Decode,
как вариант
elem = target.previousElementSibling;
elem.checked = !elem.checked;
vBulletin® v3.6.7, Copyright ©2000-2025, Jelsoft Enterprises Ltd. Перевод: zCarot