Page 1 of 1

Populate value of a dropdown based on another dropdown value change

Posted: 2019-08-27 17:29
by lawrencekeru
Hi Gurus,

When Invoice No below is selected, I want to populate value of Tenant.

Am using the below code

Code: Select all

$j(document).on('change','#invoice_no-container', function(){
	var invoiceid = $j('#invoice_no').val();
	// console.log(invoiceid);
	if ( invoiceid == '{empty_value}' ) {
		$j('#tenant').val('');
	}else{
		$j.ajax({
			url: 'ajax_payment.php',
			data: { invoiceid:invoiceid },
			success: function(resp){
				var response = JSON.parse(resp)
				console.log(response.tenantid);
				$j('#tenant').val(response.tenantid).trigger('change');
			}
		});
	}
});
The Tenant dropdown is not populating the name.

When I inspect the element, the value is set as below

Code: Select all

<input type="hidden" name="tenant" id="tenant" value="27">
Please assist.

Re: Populate value of a dropdown based on another dropdown value change

Posted: 2019-08-28 09:08
by pbottcher
Hi,

is the tenant a single selection field, or is it bound in appgini to the Invoice No?

If it is a single field (which I assume now). Is the value you want to display in the dropdown the same that you want to have in the field? Like the 27 in your example?

If that is the case you can try

Code: Select all

		$j.ajax({
			url: 'ajax_payment.php',
			data: { invoiceid:invoiceid },
			success: function(resp){
				var response = JSON.parse(resp)
				console.log(response.tenantid);
				$j('#tenant').val(response.tenantid).trigger('change');
				$j('#tenant').text(response.tenantid); 
				$j('#s2id_tenant-container').find('.select2-chosen').text(response.tenantid);
				AppGini.current_tenant.value=response.tenantid;			}
		});

Re: Populate value of a dropdown based on another dropdown value change

Posted: 2019-08-28 13:06
by lawrencekeru
Hi pbottcher,

Its works well, thanks sir!