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 Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Apr 17, 2018 in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

763 views 2 19
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