jQuery get checkbox value if checked and remove value when unchecked
You don't need an interval, everytime someone changes the checkbox the change
event is fired, and inside the event handler you can change the HTML of #show
based on wether or not the checkbox was checked :
$('#check').on('change', function() {
var val = this.checked ? this.value : '';
$('#show').html(val);
});
FIDDLE
Working Demo http://jsfiddle.net/cse_tushar/HvKmE/5/
js
$(document).ready(function(){
$('#check').change(function(){
if($(this).prop('checked') === true){
$('#show').text($(this).attr('value'));
}else{
$('#show').text('');
}
});
});
There is another way too:
Using the new property method will return true or false.
$('#checkboxid').prop('checked');
Using javascript without libs
document.getElementById('checkboxid').checked
Using the JQuery's is()
$("#checkboxid").is(':checked')
Using attr to get checked
$("#checkboxid").attr("checked")
i prefer second option.