Why does Javascript non always pre-populate Summary field

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

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.

So far so good! THX!

I spoke too soon. :( Not fixed.

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

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.

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.

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.

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?

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.

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

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?

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

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

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

That would be okay.

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Jira

Looking for anyone who made the switch to Data Center

The Jira Marketing team is putting together an ebook on migrating to Data Center. We're looking for pro tips on how you staffed your project team and organized your Proof of Concept. Share yo...

24 views 0 2
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