Page 1 of 1
Problem with Hide Field on Tabs
Posted: 2020-06-04 16:17
by lzonca
hello, I have a DV with some Tabs, I set the code to hide or not a field based on a choice from the option list. the system works, but when I save and return to the Tabs, the hidden field reappears. how can I do ??
thanks
this a code on js file
$j(function(){
$j('#accompagnatore').on('change', function(){
if($j('#accompagnatore').select2('val') == 'NO'){
$j('#note_accompagnatore').parents('.form-group').hide();
}else{
$j('#note_accompagnatore').parents('.form-group').show();
$j('#note_accompagnatore').focus().select();
}
})
})
Re: Problem with Hide Field on Tabs
Posted: 2020-06-04 20:07
by pfrumkin
You need something outside the onchange to init the page. The onchange just works off the change event and no event, no hide/show. It's basically that same code segment in the body of the onchange, the if block, but outside the onchange.
Try
$j(function(){
if($j('#accompagnatore').select2('val') == 'NO'){
$j('#note_accompagnatore').parents('.form-group').hide();
}else{
$j('#note_accompagnatore').parents('.form-group').show();
}
$j('#accompagnatore').on('change', function(){
if($j('#accompagnatore').select2('val') == 'NO'){
$j('#note_accompagnatore').parents('.form-group').hide();
}else{
$j('#note_accompagnatore').parents('.form-group').show();
$j('#note_accompagnatore').focus().select();
}
})
Re: Problem with Hide Field on Tabs
Posted: 2020-06-06 16:23
by lzonca
hi, thanks for your interest.
i inserted the code in my js file, but nothing does not work.
I don't know if it depends on the fact that this select is inside a tabs.
this is only code in table-dv.js
$j(function(){
if($j('#accompagnatore').select2('val') == 'NO'){
$j('#note_accompagnatore').parents('.form-group').hide();
}else{
$j('#note_accompagnatore').parents('.form-group').show();
}
$j('#accompagnatore').on('change', function(){
if($j('#accompagnatore').select2('val') == 'NO'){
$j('#note_accompagnatore').parents('.form-group').hide();
}else{
$j('#note_accompagnatore').parents('.form-group').show();
$j('#note_accompagnatore').focus().select();
}
})
Re: Problem with Hide Field on Tabs
Posted: 2020-06-06 17:24
by pbottcher
Hi,
you can try to set a timer to see if it is because the select2 element does not yet exist.
Re: Problem with Hide Field on Tabs
Posted: 2020-06-19 16:07
by lzonca
Thanks to Ahmed (UDEMY COURSE) to resolve it.
this is the code to resolve my problem.
Code: Select all
$j(function(){
setInterval(function() {
if(
$j('#accompagnatore').select2('val') == 'NO' &&
!$j('#note_accompagnatore').parents('.form-group').hasClass('hidden')
) {
$j('#note_accompagnatore').parents('.form-group').addClass('hidden');
}
if(
$j('#accompagnatore').select2('val') != 'NO' &&
$j('#note_accompagnatore').parents('.form-group').hasClass('hidden')
) {
$j('#note_accompagnatore').parents('.form-group').removeClass('hidden');
$j('#note_accompagnatore').focus().select();
}
}, 500);
})
The above code will make a check every 500 msec for the value of the drop-down and hide/show the field accordingly.