Custom field Javascript migration from Jira 4.4 to 5.1

MIKE VEILLEUX July 22, 2012

Hi,

Currently using Jira 4.4, we have a custom field than is populating 2 other fields using onchange.

We plan to upgrade to Jira 5.1, but the javascript isn't working anymore as Jira5 doc mentions, which is a lillte sad...

Anyway, would anyone be kind enough to help me (and probably many other users) make the following code work in Jira 5.1.

<script type="text/javascript">
            function generateURL() {
                var sel = document.getElementById('customfield_10023');
                var selText = sel.options[sel.selectedIndex].text;
                var Fieldtoreadonly = document.getElementById("customfield_10023");

                selText = selText.substr(selText.lastIndexOf('(') + 1);
                selText = selText.substr(0, selText.indexOf(')'));

                document.getElementById('customfield_10761').value = 'http://intranet/wiki/index.php/' + selText ;


switch (selText) {
case '4464': document.getElementById('customfield_10763').value = 'Aviser direction si support pour emballeuse (interface)' ;
break;
case '4564': document.getElementById('customfield_10763').value = 'Aviser direction si support pour emballeuse (interface)' ;
break;
case '4470': document.getElementById('customfield_10763').value = 'Aviser direction si support pour emballeuse (interface)' ;
break;
case '4467': document.getElementById('customfield_10763').value = 'Aviser direction si support pour emballeuse (interface)' ;
break;

default:document.getElementById('customfield_10763').value = '';
break;
}

            }

            window.onload = function() {
                document.getElementById('customfield_10023').onchange = generateURL;
            }
        </script>



In advance, thank you!

6 answers

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.
September 4, 2012

The Best way to use Javascript in jira is by using webresource, check the following post

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

0 votes
John Burns
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.
September 3, 2012

This is how i did it:

<script type="text/javascript">
(function($) {   
   
    AJS.toInit(function(){
		//function  
    });
   
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context) {
		//function  
    });
})(AJS.$);
 </script>

Harry Chan
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.
September 4, 2012

I got it working this way too.

JoeR August 26, 2013

Have same issue, want to be able to have javascript fire for creates and edits. But trying to understand this code...would })(AJS.$); in John's code example be the following to work for Mike's issue?

})(AJS.$('#customfield_10023').live("change",generateURL));

or ?

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.
August 27, 2013

you have to bind your code in NEW_CONTENT_ADDED event

JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
alert("Hello!!!");
});

i have given link in my answer!!

0 votes
MIKE VEILLEUX August 30, 2012

Hi,

thanks for the suggestion, and sorry for the delay, it's vacation season!!

But i'm a little lost, would someone be kind enough to have the code in first post adapted to work with the jquery .live method?

in advance, thank you

Florin Manaila
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.
September 2, 2012
window.onload = function() {
   document.getElementById('customfield_10023').onchange = generateURL;
}

becomes

window.onload = function(){
    AJS.$('#customfield_10023').live("change",generateURL);
}

MIKE VEILLEUX September 3, 2012

Florin, Thanks for the reply,

The code works, but only if I open the form in a new window still, the create/edit popup doesn't call the code, still the same situation...

regards

0 votes
Florin Manaila
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.
July 24, 2012

You could use the .live() method from jQuery (AJS).

http://api.jquery.com/live/

0 votes
Florin Manaila
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.
July 24, 2012

You could use the .live() method from jQuery (AJS).

http://api.jquery.com/live/

0 votes
Dieter
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.
July 22, 2012
MIKE VEILLEUX July 22, 2012

Thanks for the reply,

I tried that but it does not work when creating/updating the issue from the "popup"

If I open the edit window in a new tab, then it works as expected...

Suggest an answer

Log in or Sign up to answer