Custom field Javascript migration from Jira 4.4 to 5.1

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

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...

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

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

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

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

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

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

becomes

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

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

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>

I got it working this way too.

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 ?

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!!

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

688 views 6 13
Read article

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