Why does Javascript non always pre-populate Summary field

DI2E Licensing
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.
January 26, 2015

In a field configuration for one of our projects, we have the following in the Summary description:

 

<script type="text/javascript">
AJS.$("#summary").val("Group Membership Request");
</script>

In the create screen, sometimes the Summary field gets pre-populated and sometimes is doesn't.

Any ideas?

 I don't know javascript to save my life.  I copied and pasted this from some other source.  I'm happy to solve this some other way.   

Thanks.

4 answers

0 votes
Joel Chuah
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.
January 26, 2015

Have you tried putting your code in the announcement banner instead?

0 votes
DI2E Licensing
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.
January 26, 2015

That would be okay.

0 votes
Alexej Geldt
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.
January 26, 2015

btw. this might also overwrite the summary on edit issue page.

0 votes
Alexej Geldt
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.
January 26, 2015

possible reason:

your javascript code is running when the DOM document (page) is still rendering.

Sometimes youre lucky and the element with id #summary has already been rendered. Then your script is picking it up and rewriting the value as intended.

Sometimes however it is possible that the #summary element has not been rendered when your script is running. Therefore it is unable to locate the #summary element and does nothing.

 

possible solution:

try embracing the population code into document.ready event handler.

this ensures, that the script is running only when the page has finished rendering.

&lt;script type="text/javascript"&gt;
AJS.$(document).ready(function() {
    AJS.$(document).bind('dialogContentReady', function(event, dialog) {
        AJS.$("#summary").val("Group Membership Request");                
    });
});
&lt;/script&gt;

PS: Elements that appear in pop up dialogs are missing even when document is finished rendering.

To pick them you should bind your preopulation code to dialogContentReady event.

Try this.

DI2E Licensing
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.
January 26, 2015

So far so good! THX!

DI2E Licensing
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.
January 26, 2015

I spoke too soon. :( Not fixed.

Alexej Geldt
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.
January 26, 2015

same behavior or doesn't work at all? are there any javascript errors in the browser console?

Alexej Geldt
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.
January 26, 2015

i have changed the example code for your-temlate. Try binding your prepopulation code to dialogContentReady event. If the summary field is coming in a popup dialog, it is accessable only after dialogContentReady event.

Alexej Geldt
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.
January 26, 2015

also watch out for javascript console of your browser. It often tells you what is wrong. if it cannot pick element by id, you will see an error there.

DI2E Licensing
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.
January 26, 2015

The new code did not work. In fact, it wouldn't render anything at all. I went back to my original code and now it won't fail. I'm sure it will eventually. No errors from javascript console when using your new javascript.

Alex Medved _ConfiForms_
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.
January 26, 2015

Are you sure you don't have more than one element with ID "summary" on the page? Can you debug it, as when it does not set the field, try to open console (like in Chrome Developer tools) and type AJS.$('#summary') How many elements you will get back?

DI2E Licensing
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.
January 27, 2015

I tried this. I only got one element back. [<input class=​"text long-field" id=​"summary" name=​"summary" type=​"text" value>​] It did not populate the Summary field btw. I have noticed that it seems to always work after I have brought up one create pop-up and either canceled it or created the new issue. The next time the create pop-up arrives, the summary field is populated. Furthermore, when it is correctly populating the summary field, it in-correctly populates the summary field for every issue I go to create. Regardless of project or issue type. Even tho this javascript is in the description for Summary in a field configuration only used for one issue type in one project. I'm definitely doing something wrong.

Alexej Geldt
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.
January 27, 2015

>I have noticed that it seems to always work after I have brought up one create pop-up and either canceled it or created the new issue. The next time the create pop-up arrives, the summary field is populated. this clearly indicates that my assumption was right. Your script is running at wrong time, when the element does not exist. let me explain: all the popup dialogs, (such as create-issue or edit-issue) are being rendered dynamically when user clicks according button. Once rendered, they are not getting removed, but hidden. They are still there but unvisible after first call. first time your script is running after rendering issue-view page when the #summary element is missing. Nothing happens. once you open a pop-up (click edit), the content of the dialog is rendered into the page and #summary appears for first time. But there is no javascript action going on at this time. once you have canceled a popup, it remains in the background. So when your script is being processed next time, the summary element is available.

DI2E Licensing
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.
January 27, 2015

How do I fix this? Here's what I want. For this issue type, in this project, I want to pre-populate the summary field. What's the best way of doing that?

Alexej Geldt
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.
January 27, 2015

well populating summary for specific issue type and specific project will require more logic then just setting value for #summary. the code i have posted should make it possible to synchronize script processing with popup dialogs. there might be something else broken which prevents you from running the script. I would suggest declaring the script as ressource in global context. Then you can debug it and see if it is running at all, when it is running and whether it can see #summary element during runtime. you do it by putting the java code into .js file in main/resources folder and declaring it as webresource in main/atlassian-plugin.xml see documentation https://developer.atlassian.com/display/JIRADEV/Web+Resource+Plugin+Module

Alexej Geldt
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.
January 27, 2015

alternatively you can use javascript file that is already declared as default webresource for your plugin. just dont forget to give it a global context. Otherwise the script will not be loaded into default jira pages. Something like <context>atl.global</context> more about context https://answers.atlassian.com/questions/10343/web-resource-contexts

Suggest an answer

Log in or Sign up to answer