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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

134 views 0 3
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