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

This widget could not be displayed.

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

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

This widget could not be displayed.

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

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

This widget could not be displayed.

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 widget could not be displayed.

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

This widget could not be displayed.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

85 views 1 0
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