jquery - javascript get the value of checkbox if checked without button clicked -
javascript value of checkbox if checked without button clicked
i struck in middle of project, please help..
how check box value class name or name or id through alert particular value can pass...
i sorry ask..i new jquery , javascript..
this html ...
<input type="checkbox" name='cbox' value="red" class="theclass"/>red <input type="checkbox" name='cbox' value="green" class="theclass"/>green <input type="checkbox" name='cbox' value="yer" class="theclass"/>yer <input type="checkbox" name='cbox' value="asdasd" class="theclass"/>asdasd <input type="checkbox" name='cbox' value="radgdfged" class="theclass"/>radgdfged $(function(){ $('input[type=checkbox]').on('change', function() { alert($(this).val()); console.log($(this).val()); }); });
i googled ...but every onclick button checkbox values.. getting values using onclick button ....but thing getting value without using button... my concern getting value if checkbox checked..
eg: if checked red , should alert 'red'.....
jquery:
$('input[type=checkbox]').on('change', function() { console.log($(this).val()); });
javascript:
var cbs = document.queryselectorall('input[type=checkbox]'); for(var = 0; < cbs.length; i++) { cbs[i].addeventlistener('change', function() { console.log(this.value); }); }
edit:
if want value if checkbox checked.
jquery:
$('input[type=checkbox]').on('change', function() { if($(this).is(':checked')) console.log($(this).val()); });
javascript:
var cbs = document.queryselectorall('input[type=checkbox]'); for(var = 0; < cbs.length; i++) { cbs[i].addeventlistener('change', function() { if(this.checked) console.log(this.value); }); }
p.s. jqyery put code inside:
$(function() { //code here });
Comments
Post a Comment