Change tab name as soon as a certain value is selected

Hello,

I would like my tab name to change accordingly when I select a value in a dropdown.
It works with this code, but I would like to make it more efficient.
This is the working code:

function changeTab(){
           if(fd.field('Selection_x0020_of_x0020_Busines').value == 'Customer SD'){
                fd.container('Tab1').tabs[1].title = 'Customer SD'
            } else if(fd.field('Selection_x0020_of_x0020_Busines').value == 'Vendor'){
                fd.container('Tab1').tabs[1].title = 'Vendor'
            } else if(fd.field('Selection_x0020_of_x0020_Busines').value == 'Ship-to'){
               fd.container('Tab1').tabs[1].title = 'Ship-to'
            } else if(fd.field('Selection_x0020_of_x0020_Busines').value == 'Debtor FI'){
                fd.container('Tab1').tabs[1].title = 'Debtor FI'
            } else if(fd.field('Selection_x0020_of_x0020_Busines').value == 'Architects, planer'){
                fd.container('Tab1').tabs[1].title = 'Architects, planer'
            } else if(fd.field('Selection_x0020_of_x0020_Busines').value == 'Competitor'){
               fd.container('Tab1').tabs[1].title = 'Competitor'
            } else if(fd.field('Selection_x0020_of_x0020_Busines').value == 'Prospect'){
                fd.container('Tab1').tabs[1].title = 'Prospect'
            } else if(fd.field('Selection_x0020_of_x0020_Busines').value == 'Other BP'){
               fd.container('Tab1').tabs[1].title = 'Other BP'
            } else {
                fd.container('Tab1').tabs[1].title = ''
            }  
    }

fd.field('Selection_x0020_of_x0020_Busines').$on('change',changeTab);
changeTab();

This the new one. Could you tell me how to modify the code accordingly so that it does what the top one does?

function changeTab(){
        var i =0;
        for (i=0; i < fields.length; i =i+1){
        	if(fd.field('Selection_x0020_of_x0020_Busines').value == fields[i]){
                    fd.container('Tab1').tabs[1].title = fields[i]
        	} else {
        	   fd.container('Tab1').tabs[1].title = ''
                }
        }
    }

Thank you and best regards

Dear @Sternchen,
What exactly is fields array? Is it an array of field values? Is it an array of field objects? What does it hold?

If it's all the fields you've referenced before, you might just need to check the value:

function changeTab(){
        var i =0;
        for (i=0; i < fields.length; i =i+1){
        	if(fd.field('Selection_x0020_of_x0020_Busines').value == fields[i].value){
                    fd.container('Tab1').tabs[1].title = fields[i].value;
        	} else {
        	        fd.container('Tab1').tabs[1].title = '';
                }
        }
    }

Sorry I forgot the array.
Here it is:
var fields = ['Customer SD', 'Vendor', 'Ship-to', 'Debtor FI', 'Architects, planer', 'Competitor', 'Prospect', 'Other BP'];

Dear @Sternchen,
In this case, I think the main issue is that it's overwritten as soon as next value is checked. You can use break and also move empty title outside of the loop:

function changeTab(){
        fd.container('Tab1').tabs[1].title = '';
        for(var i=0; i < fields.length; i++){
        	if(fd.field('Selection_x0020_of_x0020_Busines').value == fields[i]){
                fd.container('Tab1').tabs[1].title = fields[i];
                break;
        	} 
        }
}
1 Like

That worked. Thank you.