How to include some JavaScript for a jira custom field?

I tried to add some JavaScript inside a custom field description and it worked well inside the custom field editor.

Still, it seems that Jira stripped my javascript from the description when it was displayed in the issue UI (as apposed to the admin area, where it did not).

So, I would say this is a bug, now the question is how to workaround this and have my code executed.

3 answers

In complement to my previous intervention, I have just found how a workaround to my issue:

<script type="text/javascript">
(function($) {  
    AJS.toInit(function(){
        do stuff...
      });
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context) {
        do stuff again... 
    });
    // ------------------ Below the new line
    $(selector_of_the_element_to_modify).live("click", do stuff agin...)
})(AJS.$);
 </script>

I thought the .live would be sufficient, but the rest of the code provided by John Burns is still required: the call to .toInit and .bind handles the case when the create popup opens with the CF already shown, and the call to .live handles the case when the project/issue type needs to be changed to display the CF

Not sure how this can be used to do something else than adding handlers to specific elements, though

0 vote

Have you included it in the correct field configuration? Editing description under custom fields is not enough.

I think it should be enough, as the description I entered does appear inside the Create/Edit screen, it is updated when I modify it but it does not contain the script part `<script>...</script>`. Which, by the way is included in the list of custom fields in the admin. I think this is related to the fact that in new jira (5.x), the Create/Edit screens are created from JSON instead of being rendered on the server side.

Can you try adding it in the field configuration and see how it works? I can certainly find mine in the create/edit screens. Even on Create/Edit, rendering is done on server side.

I tried, no change the script section is removed but the rest is kept.

Try using this template:

&lt;script type="text/javascript"&gt;
(function($) {   
    AJS.toInit(function(){
        do stuff...
      });
   
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context) {
        do stuff again...  
    });
})(AJS.$);
 &lt;/script&gt;

I tried and the code does not reach the UI or Create/Edit issue. Still, anything that is outside the script tags does reach. Are you sure that you tested this with Jira 5.2?

Yes, I am on 5.2, and it works in my instance. You might need to open a support ticket on this one.

I am retrying now on a test instance by enabling the safe mode, maybe there is somethiing that does this.

John's answer worked for my case (Jira 5.0.7, trying to set onclick actions for a CF), thank you very much John !

Still there is a case when it is not enough: my CF is only available for one issue type (New feature). When the Create popup opens with another issue type selected and I change it to New Feature, the script seems not to run. Not a real problem for my case, I'll eventually try to find a workaround

Hi,

did you find a workaround? I have currently the exactly same issue and for me it _is_ a problem ;-)

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

200 views 1 18
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