How to trigger jQuery change event in Checkbox

You need to trigger the change event, .trigger(‘change’), so that event knows that a change took place.

They can be fired manually, however, with the .trigger() method. A call to .trigger() executes the handlers in the same order they would be if the event were triggered naturally by the user.

JavaScript Code

$('#typedetransport').on('change', function(){
	var value = $(this).val();
	if(value == 'Professionnel' || value == 'Scolaire')
	{
		$("#returnbox").prop("checked", true);
		$("#returnbox").trigger("change");
	}
	else
	{
		$("#returnbox").prop("checked", false);
		$("#returnbox").trigger("change");
	}
});

Automatic Change Look

$('#returnbox').on('change', function(){
	this.value = this.checked ? 1 : 0;
	if(this.checked) {
		$('#date_return').prop('required',true);
		$('#time_return').prop('required',true);

	}
	else
	{
		$('#date_return').val('');
		$('#time_return').val('');
		$('#date_return').prop('required',false);
		$('#time_return').prop('required',false);					
	}
});

Leave a Reply

Your email address will not be published. Required fields are marked *