can we use jquery in SetHelpText in Behaviours Plugin?

my jira version is jira 4.4.1

i add i validator on field "Resolution" in Behaviours plugin

FormField formField = getFieldById("resolution")
FormField descField = getFieldById("comment")

if (formField.getFormValue() == "1") { //
    descField.setHelpText("<script type=\"text/javascript\">var str=\"WWWW\";\
 AJS.$(\"#customfield_11100\").innerText=\"BBBGG\";</script>")
}
else {
    descField.setHelpText("")
}

there is compilation error:

Compilation failure: startup failed: Script1.groovy: 7: illegal string body character after dollar sign; solution: either escape a literal dollar sign "\$5" or bracket the value expression "\$5" @ line 7, column 26. descField.setHelpText("<script type=\"text/javascript\">var str=\"WWWW\";\ ^ 1 error

then i replace "$" with "\$5" or "\$5",but there is no result i want.

if i use java script other than jquery,i get the result.

FormField formField = getFieldById("resolution")
FormField descField = getFieldById("comment")

if (formField.getFormValue() == "1") { //
    descField.setHelpText("&lt;script type=\"text/javascript\"&gt;var str=\"WWWW\";\
document.getElementById(\"customfield_11100\").innerText=\"BBBGG\";&lt;/script&gt;")
}
else {
    descField.setHelpText("")
}

i am wondering if we can use jquery in groovy in SetHelpText in Behaviours Plugin?

2 answers

1 accepted

0 votes
Accepted answer

Yes you can .

I have done this before and it had worked.

For me it had worked on 4.3.2

if you use "$",don't you get any error message?

i guess "$" is one of reserved characters for groovy regular expression

i escape it "AJS.\$" .

In your script i dont see a $5 still the compiler is giving an error. you can cross check the script associated to the field

if i use "AJS.\$",there is no compilation error,but field "customfield_11100"'s value is not changed.

FormField formField = getFieldById("resolution")
FormField descField = getFieldById("comment")
 
if (formField.getFormValue() == "1") { //
    descField.setHelpText("&lt;script type=\"text/javascript\"&gt;var str=\"WWWW\";\
 AJS.\$(\"#customfield_11100\").text(str);&lt;/script&gt;")
}
else {
    descField.setHelpText("")
}

What type of customfield is customfield_11100 ? I think there is some mistake in the JQuery

What AJS.$("#customfield_11100").text(str) does ?

customfield_11100 is of Text Field type.

You have two problems.

The first is what Mizan pointed out: the $ needs to be properly escaped in the validator code you entered in the Behaviours plugin. I see you’ve fixed that in your comments.

The second is that your jQuery is wrong. The innerText attribute of a jQuery object (as opposed to an actual DOM object in IE) has no meaning. In your comments, you switched to calling the .text() method. That’s better, but it won’t do what you want on form input elements.

What you need is the .val() method. That can get and set the value that the user entered and sees.

Try this:

FormField formField = getFieldById("resolution")
FormField descField = getFieldById("comment")
  
if (formField.getFormValue() == "1") { //
    descField.setHelpText("&lt;script type=\"text/javascript\"&gt;var str=\"WWWW\";\
 AJS.\$(\"#customfield_11100\").val(str);&lt;/script&gt;")
}
else {
    descField.setHelpText("")
}

thanks,it's really my fault.

i thought it is "textarea" of type ,in fact it is "input" in html source view.

i use "val()" ,and now the field "customfield_11100" displays the value of "WWWW"

in this example ,i have another problem.

if i change resolution to other ,not the default "Fixed" option(whose value is "1"),the value of "customfield_11100" is still be "WWWW",i.e. code

else {
descField.setHelpText("")
}
is not executed at all.

This might be because it is not going in the else ,

try

if (formField.getFormValue() == 1)

hello Mizan,

if i use "if (formField.getFormValue() == 1) ","customfield_11100" 's value is not changed yet.

i think when i select "Duplicate","Incomplete","Won't Fix",and so on whose value is not "1". the code

is executed into else.but the page is not flushed , and "customfield_11100" 's value is still be there.

when i change the script to "<div class=\"warningBox\">should not choose Fixed.</div>",then result varies from selected resolution value.

FormField formField = getFieldById("resolution")
FormField descField = getFieldById("comment")
 
if (formField.getFormValue() == "1") { //
    descField.setHelpText("&lt;div class=\"warningBox\"&gt;should not choose Fixed.&lt;/div&gt;")
}
else {
    descField.setHelpTex("")
}

No, of course the value of “customfield_11100” doesn’t change when you change the input to the “Resolution” field away from “Fixed.” The “else” clause is, in fact, being executed, but there’s nothing in that clause that changes the value of “customfield_11100!”

All the “else” clause is doing is clearing the help text of the “comment” field. It’s not loading any Javascript, and it certainly won’t magically undo the Javascript that was there before. If you want it to clear the value of “customfield_11100,” you’re going to have to include some Javascript to do so.

thanks,Cedric

when i add the clear value javascript to the "else" clause.it's really shows as expected.

FormField formField = getFieldById("resolution")
FormField descField = getFieldById("comment")
 
if (formField.getFormValue() == "1") { //
    descField.setHelpText("&lt;script type=\"text/javascript\"&gt;var str=\"WWWW\";\
 AJS.\$(\"#customfield_11100\").val(str);&lt;/script&gt;")
}
else {
    descField.setHelpText("&lt;script type=\"text/javascript\"&gt;var str=\"WWWW\";\
 AJS.\$(\"#customfield_11100\").val(\"\");&lt;/script&gt;")
}

Cedric,

can i ask another question,the language Groovy is a "server side" or "client side" script?

according to the script used above,i guess it's client side.

The person you really should be asking is Jamie Echlin, who wrote the plugin and contributes to this forum. I’m just a guy who has used the plugin before. But I’ll do what I can.

It’s actually a little complicated. As you noted, the script cause the web page to change dynamically as the user manipulates the form input elements. That certainly indicates a client-side script. But, in fact, the Groovy code you wrote runs on the server, so it’s a server-side script!

How can this be? The Behaviours plugin inserts client-side Javascript into the edit page. As the user manipulate form fields, this client-side script makes out-of-band HTTP calls to the server. That causes the server to run your Groovy code and send a response back to the client-side script. The client-side script parses that response and dynamically updates the page.

In this case, you could have done this using just Javascript/jQuery. It’s not a particularly structured solution, but it could work. Personally, I’m more familiar with Javascript than I am with Jira, so I probably would have gone with just Javascript/jQuery. But you might find it easier and more structured to use the Behaviour plugin.

But because the Groovy script runs server-side, you can actually do more with it than you could with a client-only script. The Javascript code, by itself, is limited to gathering information already on the page. Using a Groovy script, you can gather additional information. For example, you couldn’t easily generate a list of all the leads of the components of the issue with just Javascript, but you could do it with a few lines of server-side Groovy code.

Cedric has put this better than I could myself.

Sorry I have not been chipping in, I am having a little break from AAC as I have too much other non-work work for a few weeks.

I totally agree that writing javascript back into the helptext is not very structured, and is likely to lead to something hard to debug/maintain. I have not really understood why this is required, but if it is, perhaps it's the best available point solution.

I would make a slight clarification. Cedric mentions that client script can get information on the page, however with recent versions it can make use of REST APIs. But, when I wrote this plugin (around 3.10), this was the primary motivation, as the rest apis weren't available. So getting the users groups or roles in the current project for instance was not possible. However, that has all changed now.

The other driver was that the server-side scripts should only need to be written once. I handle the necessary javascript changes as jira evolves. If you write javascript yourself you would have needed at least two significant rewrites since 3.x.

IMHO there are still good reasons to use the plugin, although they are not nearly as compelling as they were way back. Hence my reconsidering the future of this plugin.

Many thanks to Cedric and Mizan for input on this question.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 16, 2018 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...

1,390 views 17 10
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