JavaScript to Hide/Show Custom Field

I have a javascript at a custom fields to hide/show the field based on another field value.
It works fine when I create an issue.
The problem is when I change the project or the issue type. When I do this it seems that I don't have a javascript.
One of the javascripts that I use is:
<script>
checkbox_11106_1 = document.getElementById('customfield_11106-1'); //New - Yes
checkbox_11106_2 = document.getElementById('customfield_11106-2'); //New - No

if (!checkbox_11106_1.checked){
	AJS.$("#customfield_11118").closest('div.field-group').show();
}
if (!checkbox_11106_2.checked){
	AJS.$("#customfield_11118").closest('div.field-group').hide();
	document.getElementById('customfield_11118').value = '';
}

checkbox_11106_1.onclick=function(){
	if (checkbox_11106_1.checked){
		AJS.$("#customfield_11118").closest('div.field-group').hide();
		document.getElementById('customfield_11118').value = '';
	}
};
checkbox_11106_2.onclick=function(){
	if (checkbox_11106_2.checked){
		AJS.$("#customfield_11118").closest('div.field-group').show();
	}
};
</script>

How can I resolve this problem?

4 answers

1 accepted

Now it works. I'm using this script at the custom field

<script>
function newField(){
		if (document.getElementById('customfield_11106-1').checked){
			AJS.$("#customfield_11118").closest('div.field-group').hide();
			document.getElementById('customfield_11118').value = "";
		}
		if (document.getElementById('customfield_11106-2').checked){
			AJS.$("#customfield_11118").closest('div.field-group').show();
		}
	
		AJS.$('#customfield_11106-1').click(function(){
			if (document.getElementById('customfield_11106-1').checked){
				AJS.$("#customfield_11118").closest('div.field-group').hide();
				document.getElementById('customfield_11118').value = "";
			}
		});
		
		AJS.$('#customfield_11106-2').click(function(){
			if (document.getElementById('customfield_11106-2').checked){
				AJS.$("#customfield_11118").closest('div.field-group').show();
			}
		});
}
AJS.$(document).ready(function(){newField()});
AJS.$(document).bind('dialogContentReady', function(event, dialog){newField()});
JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function(e,context){newField()});
newField();
</script>

And using this script at the announcement banner

<script type="text/javascript">
AJS.$("#create_link").removeClass("create-issue");
</script>

Hi Ana,

I am able to use your example script, but my JIRA hangs up when I create a project/issue type that does not have the two custom fields in the create screen. Can you help? Thanks

This is the script:

<script>

function newField(){

        if (document.getElementById('customfield_10201').value != 35){

            AJS.$("#customfield_11201").closest('div.field-group').hide();

            document.getElementById('customfield_11201').value = "";

        }

        if (document.getElementById('customfield_10201').value == 35){

            AJS.$("#customfield_11201").closest('div.field-group').show();

        }

     

        AJS.$('#customfield_10201').click(function(){

            if (document.getElementById('customfield_10201').value != 35){

                AJS.$("#customfield_11201").closest('div.field-group').hide();

                document.getElementById('customfield_11201').value = "";

            }

        });

         

        AJS.$('#customfield_10201').click(function(){

            if (document.getElementById('customfield_10201').value == 35){

                AJS.$("#customfield_11201").closest('div.field-group').show();

            }

        });

}

AJS.$(document).ready(function(){newField()});

AJS.$(document).bind('dialogContentReady', function(event, dialog){newField()});

JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function(e,context){newField()});

newField();

</script>

 

Nevermind we Fix it. We added a Null for the value.

 

0 vote
Henning Tietgens Community Champion May 07, 2014

For properly working javascript, you have to provide it as a web ressource plugin. See https://answers.atlassian.com/questions/287928/adding-javascript-to-hide-and-prefil-summary-while-creating-an-issue

Henning

Hi Ana,

Maybe this modification fixes your problem:

&lt;script&gt;
(function($) {
    checkbox_11106_1 = document.getElementById('customfield_11106-1'); //New - Yes
    checkbox_11106_2 = document.getElementById('customfield_11106-2'); //New - No
     
    if (!checkbox_11106_1.checked){
        AJS.$("#customfield_11118").closest('div.field-group').show();
    }
    if (!checkbox_11106_2.checked){
        AJS.$("#customfield_11118").closest('div.field-group').hide();
        document.getElementById('customfield_11118').value = '';
    }
     
    checkbox_11106_1.onclick=function(){
        if (checkbox_11106_1.checked){
            AJS.$("#customfield_11118").closest('div.field-group').hide();
            document.getElementById('customfield_11118').value = '';
        }
    };
    checkbox_11106_2.onclick=function(){
        if (checkbox_11106_2.checked){
            AJS.$("#customfield_11118").closest('div.field-group').show();
        }
    };
})(AJS.$);
&lt;/script&gt;

Hi,

there is a lot of answer to questions like yours. among it this, you must just replace ids of your custom fields.

https://answers.atlassian.com/questions/264320/pop-up-a-user-picker-window

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,264 views 14 20
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot