Page 1 of 1

Show/Hide field based on lookup

Posted: 2019-04-17 18:48
by kbarrett
Hi all,

I likely am way of with this but I am trying to show hide a field or two based on one value from a lookup field. I thought I would create a tablename-dv.js file with the following code in it. I have tried both text and a numerical value of the text selection. Neither works:

$j(document).ready(function(){
$j('#My_Field').change(function() {
if($j('#My_Field').text()=="This Value")
$j('#ScID').show();
else
$j('#ScID').hide();

})

})


I have used the code from Ahmed's tutorials but it does the opposite and hides the field when the lookup is selected:

$j(function(){
$j('#My_Field').on('change', function(){
var IR = parseFloat($j(this).val());

if(IR == 'This Value'){
$j('#ScID').parents('.form-group').show();
}else{
$j('#ScID').parents('.form-group').hide();
}
});
});

I am not sure what I have to do, I did try and use the value of the selection (7) as well.

Re: Show/Hide field based on lookup

Posted: 2019-04-17 19:31
by pbottcher
Hi Kevin,

if you need only to check for the change you can use:

Code: Select all

$j(function(){
$j('#My_Field').on('change', function(){
var IR = $j('#s2id_My_Field-container .select2-chosen').text());

if(IR == 'MYVALUE'){
$j('#ScID').parents('.form-group').show();
}else{
$j('#ScID').parents('.form-group').hide();
}
});
});
replace My_Field by your fieldname and MYVALUE by the selected item from the lookup.

Re: Show/Hide field based on lookup

Posted: 2019-04-17 19:55
by kbarrett
Hi,

I substituted my values and it works in reverse, every selection hides the ScID field. Ideally the field stays hidden and selecting 'Safety Concern' shows the ScID field:

$j(function(){
$j('#Inspection_Reason').on('change', function(){
var IR = $j('#s2id_Inspection_Reason-container .select2-chosen').text();

if(IR == 'Safety Concern'){
$j('#ScID').parents('.form-group').show();
}else{
$j('#ScID').parents('.form-group').hide();
}
});
});

Re: Show/Hide field based on lookup

Posted: 2019-04-18 13:04
by pbottcher
Hi,
not sure what the issue is. Every selection (apart "Safety Concern") will hide the ScID. Only if you select the Safety Concern, it will be shown.

This does of course not work for the initial load of the page, as you react with the "on('change'..) only on changes to the fieldvalue.

If you want to hide field once the page gets loaded, you need to check for the value at the time the page is loaded and set the field status accordingly.

Re: Show/Hide field based on lookup

Posted: 2019-04-18 21:00
by kbarrett
Yes,

Once I click on the drop down the field disappears and doesn't come back. LOL.