Show/Hide Custom field depend on Priority field value

We need to hide a certain field namely "Specify the reason of Critical Priority" (Custom Field value 10112 ) based on Priority field value. We need to show this only when Priority Value is "Critical"

We have write java script in "Specify the reason of Critical Priority" field's description to meet this expectation. Please find the javasripr below -

<script type="text/javascript">
   priority = document.getElementById('priority');
   if (priority) {
       target = document.getElementById('customfield_10112');
       // Hide the target field if priority isn't critical
       if (priority.value != 2) target.style.display='none';
   
       priority.onchange=function() {
           if (this.value == 2) {              
                      target.style.display = '';  
                      target.value="enter message here";
                   } else {
                  target.style.display='none';
           }
       }
   }
 </script>

After configuring by above mentioned procedure "Specify the reason of Critical Priority" field is showing only when "Critical" priority is selected. When "Critical" Priority is not selected this field is not showing alright but "Specify the reason of Critical Priority" Custom Field name is showing everytime. Means Field is hideing but fieldname is showing. Could you please suggest regarding this?

9 answers

1 accepted

Hi Prasad,

Thank you very much for your help. Right now Show/Hide Customfield based on Priority field value working perfectly. I have not use alert(selectedPriority) .I have used below mentioned code and it's working perfectly after clearing the browser cache -

jQuery(document).ready(function($) {
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
                showHideFieldFunction();
            });
            showHideFieldFunction();

function showHideFieldFunction(){
            showHideField();
            $('#priority').change(function() {
                showHideField();
            });
}

function showHideField(){
    var selectedPriority=$("#priority").val();
    if(selectedPriority == '2'){
        $('#customfield_10112').closest('div.field-group').show();
    }else{
        $('#customfield_10112').closest('div.field-group').hide();
    }

}
});

if my answer's helped then don't forgot to accept as a answer :)

Hi Prasad,

How to accept as a answer ?

Regards Sumit

click on tick mark on left side of the user inmage, check this on following immage

my answers also accept a answers right?

Hi,

You'll probably need to look at the ElementId of the custom field label and set the target.style.display= 'none' for it too.

Best regards,

Peter

Hi Peter,

Could you please elaborate. I am not clear. Could you please modify my above mentioned javascript and send to me. It will be more helpful for me.

Thanks for your Reply

Regards

Sumit

Hi Peter,

Any update ?

Regards

Sumit

Hi ,

It would probably be something like this :

<script type="text/javascript">
   priority = document.getElementById('priority');
   if (priority) {
       target = document.getElementById('customfield_10112');
       label = document.getElementById('Specify the reason of Critical Priority');	
       // Hide the target field if priority isn't critical
       if (priority.value != 2){
	 target.style.display='none';
    	 label.style.display='none';
       }
       priority.onchange=function() {
           if (this.value == 2) {              
                      target.style.display = '';
		      label.style.display='';  
                      target.value="enter message here";
                   } else {
                  target.style.display='none';
                  label.style.display='none';
           }
       }
   }
 </script>

Best regards,
Peter

Peter,

Thank you for your reply. But still the problem remain same. Could you please suggest ?

Regards

Sumit

Hi Sumit,

I'm sorry but I'm not really a Javascript expert, I think the my reasoning is sound (you also need to hide the label element when the priority is != 2) but I don't know exactly how to do this with javascript (especially not when I can't test it)

Best regards,

Peter

try with this javascript if you are using jira 5.x, make sure customfield id and priority selected value is correct(casesensitive)

<script type="text/javascript">  
jQuery(document).ready(function($) {
	JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {				
				showHideFieldFunction();
			});
			showHideFieldFunction();
			
function showHideFieldFunction(){
			showHideField();
			$('#priority').change(function() {		
				showHideField();
			});	
}

function showHideField(){
	var selectedPriority=$("#priority option:selected").text();
	if(selectedPriority == 'Critical'){	
		$('#customfield_10112').closest('div.field-group').show();
	}esle{
		$('#customfield_10112').closest('div.field-group').hide();
	}

}
});
</script>

if this function need call it on create screen then i suggest to load it as a webresource module in a plugin rather than adding n description

check this

https://answers.atlassian.com/questions/47843/strange-javascript-problem-in-create-screen

Hi Prasad,

Thank you for your reply. I am using websource module for this purpose. In that case I am writing below mentioned script in JS file for build plugin. Please find the sript below -

jQuery(document).ready(function($) {
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {            
                showHideFieldFunction();
            });
            showHideFieldFunction();
             
function showHideFieldFunction(){
            showHideField();
            $('#priority').change(function() {     
                showHideField();
            });
}
 
function showHideField(){
    var selectedPriority=$("#priority option:selected").text();
    if(selectedPriority == 'Critical'){
        $('#customfield_10112').closest('div.field-group').show();
    }esle{
        $('#customfield_10112').closest('div.field-group').hide();
    }
 
}
});

But after that when we try to create the plugin using atlas-package following error showing -

org.mozilla.javascript.EvaluatorException: Compilation produced 3 syntax errors.

Could you please confirm wheather I am putting right code in js file or not ? The values of this script is correct

Regards

Sumit

the above code looking good, can you share some more log message

Please find the complete error -

[INFO] Trace
org.mozilla.javascript.EvaluatorException: Compilation produced 3 syntax errors.
        at com.atlassian.maven.plugins.amps.util.minifier.YUIErrorReporter.runti                                                                                                                                                             meError(YUIErrorReporter.java:35)
        at org.mozilla.javascript.Parser.parse(Parser.java:392)
        at org.mozilla.javascript.Parser.parse(Parser.java:337)
        at com.yahoo.platform.yui.compressor.JavaScriptCompressor.parse(JavaScri                                                                                                                                                             ptCompressor.java:312)
        at com.yahoo.platform.yui.compressor.JavaScriptCompressor.<init>(JavaScr                                                                                                                                                             iptCompressor.java:533)
        at com.atlassian.maven.plugins.amps.util.minifier.ResourcesMinifier.yuiJ                                                                                                                                                             sCompile(ResourcesMinifier.java:228)
        at com.atlassian.maven.plugins.amps.util.minifier.ResourcesMinifier.proc                                                                                                                                                             essJs(ResourcesMinifier.java:164)
        at com.atlassian.maven.plugins.amps.util.minifier.ResourcesMinifier.proc                                                                                                                                                             essResource(ResourcesMinifier.java:61)
        at com.atlassian.maven.plugins.amps.util.minifier.ResourcesMinifier.mini                                                                                                                                                             fy(ResourcesMinifier.java:40)
        at com.atlassian.maven.plugins.amps.MavenGoals.compressResources(MavenGo                                                                                                                                                             als.java:504)
        at com.atlassian.maven.plugins.amps.CompressResourcesMojo.execute(Compre                                                                                                                                                             ssResourcesMojo.java:33)
        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPlugi                                                                                                                                                             nManager.java:483)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(Defa                                                                                                                                                             ultLifecycleExecutor.java:678)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLi                                                                                                                                                             fecycle(DefaultLifecycleExecutor.java:540)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(Defau                                                                                                                                                             ltLifecycleExecutor.java:519)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHan                                                                                                                                                             dleFailures(DefaultLifecycleExecutor.java:371)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegmen                                                                                                                                                             ts(DefaultLifecycleExecutor.java:332)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLi                                                                                                                                                             fecycleExecutor.java:181)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:356)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:137)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:356)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.                                                                                                                                                             java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces                                                                                                                                                             sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)

Hi Prasad,

Actually there is a spelling mistake in my code. After correcting the code plugin build successfully. But after uploading this plugin in Jira problem remain same. Could you please look into this

Regards

Sumit

priority field is a syatem field and options are 1,2,...,5, so you have to replace "Critical" with correct value

Hi Prasad,

I have put the Critical value in my javascript and build the plugin again. But problem still remain same. Right now required field not showing even selecting 'Critical' from Priority field. Please find my recent code below and inform there is any problem -

jQuery(document).ready(function($) {
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
                showHideFieldFunction();
            });
            showHideFieldFunction();

function showHideFieldFunction(){
            showHideField();
            $('#priority').change(function() {
                showHideField();
            });
}

function showHideField(){
    var selectedPriority=$("#priority option:selected").text();
    if(selectedPriority == '2'){
        $('#customfield_10112').closest('div.field-group').show();
    }else{
        $('#customfield_10112').closest('div.field-group').hide();
    }

}
});

replace no 15 line with this

var selectedPriority=$('#priority').val();

and also give alerts and check wheather this script executing or not

Still problem not resolved. Right now required field not showing even choosing Critical Priority. Please find the updated javascript below -

jQuery(document).ready(function($) {
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
                showHideFieldFunction();
            });
            showHideFieldFunction();

function showHideFieldFunction(){
            showHideField();
            $('#priority').change(function() {
                showHideField();
            });
}

function showHideField(){
    var selectedPriority=$("#priority").val();
    if(selectedPriority == '2'){
        $('#customfield_10112').closest('div.field-group').show();
    }else{
        $('#customfield_10112').closest('div.field-group').hide();
    }

}
});

give following alert after 15th line

alert(selectedPriority);

remove browser cache and try!! it should work!

so was the problem the browser cache, been following this thread trying to figure out what was the initial problem with the initial code. if u use your first code and clear cache does it work

Hi Chris,

It did work when I used below mentioned code and clear the browser cache -

jQuery(document).ready(function($) {
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
                showHideFieldFunction();
            });
            showHideFieldFunction();
 
function showHideFieldFunction(){
            showHideField();
            $('#priority').change(function() {
                showHideField();
            });
}
 
function showHideField(){
    var selectedPriority=$("#priority").val();
    if(selectedPriority == '2'){
        $('#customfield_10112').closest('div.field-group').show();
    }else{
        $('#customfield_10112').closest('div.field-group').hide();
    }
 
}
});

you need to click on tick mark which is left side of the user image

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

58 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you