I’m having a bit strange problem, I have a form with a checkbox, where Jquery has to adjust values in an input field. But, when I check with console.log($('#praktijk').is(':checked'));
I get a neat true or false answer, no problem there.
But when I put it in an if statement like this $('#praktijk').is(':checked')
it doesn’t work, for some vague reason.
For more interpretation I’ll give the whole jscript code:
$('#praktijk').on('change', function() {
let a = $('input[name="aantal"]').val() * $('input[name="lesprijs"]').val();
var checked = $('#praktijk').is(':checked');
console.log($('#praktijk').is(':checked'));
if ($('#praktijk').is(':checked')) {
let b = Number($('input[name="examenprijs"]').val());
} else {
let b = 0;
}
let prijs = a + b;
$('input[name="pakketprijs"]').val(prijs);
});
I’m really confused about this, and hope someone can help me.
Grts
3
Answers
Seems you are declaring ‘b’ variable inside the if block and trying to access it outside.
Declare the variable before if as,
Ex.,
You need to define
b
variable out of the if.This is the corrected code:
When you declare a variable inside an if or else block using
let
, it is only accessible within that block. In your code, you are declaringb
inside the if and else blocks, and trying to use it outside those blocks.