Show/Hide Custom field depend on Priority field value

Sys Admin October 30, 2013

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

0 votes
Answer accepted
Sys Admin November 8, 2013

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();
    }

}
});

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 8, 2013

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

Sys Admin November 8, 2013

Hi Prasad,

How to accept as a answer ?

Regards Sumit

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 9, 2013

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

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 10, 2013

my answers also accept a answers right?

0 votes
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 9, 2013

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

0 votes
Chris Gaza November 8, 2013

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

Sys Admin November 8, 2013

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();
    }
 
}
});

0 votes
Sys Admin November 7, 2013

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();
    }

}
});

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 7, 2013

give following alert after 15th line

alert(selectedPriority);

remove browser cache and try!! it should work!

0 votes
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 6, 2013
Sys Admin November 6, 2013

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

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 6, 2013

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

Sys Admin November 7, 2013

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();
    }

}
});

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 7, 2013

replace no 15 line with this

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

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

0 votes
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 3, 2013

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

Sys Admin November 5, 2013

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

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 6, 2013

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

Sys Admin November 6, 2013

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)

0 votes
Peter Van de Voorde
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 3, 2013

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
Sys Admin November 3, 2013

Peter,

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

Regards

Sumit

Peter Van de Voorde
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 3, 2013

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

0 votes
Sys Admin November 3, 2013

Hi Peter,

Any update ?

Regards

Sumit

0 votes
Peter Van de Voorde
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 30, 2013

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

Sys Admin October 31, 2013

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

Suggest an answer

Log in or Sign up to answer