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

Accepted Answer
1 vote

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 votes
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 Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

25,699 views 2 7
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