Issues with executing simple javascript in description field

Hi,

I have following code and it works if i click on Create Issues while i look at an issue. If i click on Create Issue on the dashboard it does not work.

If i cancel the create dialog and click Create issue again i hang on the wheel of fortune. What am i doing wrong ?

<script type="text/javascript">
JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {     
 $('#summary').keyup(function() {
    var text = $(this).val(),
        count = encodeURIComponent(text).replace(/%[A-F\d]{2}/g, 'U').length;

switch (true)
    {
        case (count<50):
            $('#count').text("Characters: " + count);
            break;
        case (count>50 && count<60):
            $('#count').css("color","green");
            $('#count').text("Seems like a good length of a summary!");
            break;
        case (count>60 && count<80):
            $('#count').css("color","green");
            $('#count').text("You done ?");
            break;
        case (count>80):
            $('#count').css("color","red");
            $('#count').text("Hey! You can use the description field, it's just below, use it!");
            break;
    }
});
});
</script>
<div id="count"></div>
Summary

/donnib

3 answers

No one has an idea ?

I would like to recommend you to use your javascript as webresource - >

First try to move your js to separate file(e.g. summary.js) :

JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {     
 $('#summary').keyup(function() {
    var text = $(this).val(),
        count = encodeURIComponent(text).replace(/%[A-F\d]{2}/g, 'U').length;
 
switch (true)
    {
        case (count<50):
            $('#count').text("Characters: " + count);
            break;
        case (count>50 && count<60):
            $('#count').css("color","green");
            $('#count').text("Seems like a good length of a summary!");
            break;
        case (count>60 && count<80):
            $('#count').css("color","green");
            $('#count').text("You done ?");
            break;
        case (count>80):
            $('#count').css("color","red");
            $('#count').text("Hey! You can use the description field, it's just below, use it!");
            break;
    }
});
});

And then define it in atlassian-plugin.xml ->

<web-resource name="Resources" key="resources">
        <context>atl.general</context>
        <resource name="summary.js" type="download" location="summary.js"></resource>
    </web-resource>

Also there is another event - > JIRA.Events.NEW_PAGE_ADDED - so try it with this also - maybe it's better for your purpose.

@Lukas Maruniak thank you but i don't quite follow you. I tried reading the link u gave me but i don't see anywhere they discuss atlassian-plugin.xml. Is that a file i need to create ? How do i link the JavaScript with the summary field ?

Well, it's the main descpriptor file in which are defined all plugin modules which you created.

I think this could be helpfull for you -> https://developer.atlassian.com/display/DOCS/Set+up+the+Atlassian+Plugin+SDK+and+Build+a+Project

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,760 views 11 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot