Javascript to execute when create button is pressed in Jira agile New card window?

Bharadwaj Jannu
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

Hi

I need to execute some javascript(which restrict some customfield options according to logged-in user) when I press Create button on New Card pop-up of jira agile.

I wrote some javascript but I could not able to execute on jira new card context. Any suggestions.

6 answers

1 vote
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 12, 2014

Hello,

I had the same issue in the past. The problem is that your js code need the complete page load first. I fixed this problem using a timeout :

jQuery(document).ready(function() {
	setTimeout(myFunction,3000);
	
});

function myFunction(){
 HERE YOUR JS CODE
}

Hope this helps,

Fabio

Bharadwaj Jannu
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

This is working when I click Create button for first time.

But I require each time I press Create button.

0 votes
Bharadwaj Jannu
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

The above is Create button on New Card pop-up jira-agile.

The above is New Card button on Board.

0 votes
codelab expert
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

Use one of this constructs:

AJS.$(document).ready( function() {

 AJS.$("#gh-create").click(function() {
            alert("on button clicked");
        });

});

or

AJS.$(window).load( function() {

 AJS.$("#gh-create").click(function() {
            alert("on button clicked");
        });

});

Bharadwaj Jannu
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

The above mentioned scripts are not working.

codelab expert
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

Ok, what about this:

$("#someDivOrSomethingElse").bind("DOMSubtreeModified", function() {

    alert("DOM changed");

});

codelab expert
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

Hm, I am out of ideas...

Bharadwaj Jannu
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

I used as

AJS.$("#card-create").bind("DOMSubtreeModified", function() {
	alert("DOM changed");
});

here card-create is id of div tag of pop-up window.but not working

codelab expert
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

I think "card-create" is not correct. You should use a div which is already there and exists all over the time.

Bharadwaj Jannu
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

I used page an id present in all Jira pages but not working in the script.

codelab expert
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

And what about AJS.$("body").bind( .......

Bharadwaj Jannu
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

even the above is not showing any alert.

codelab expert
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

how many tags of "card-create" do you have? More than one? Perhaps you must use

AJS.$("#card-create").each( function(this) {

// bind click event

})

0 votes
codelab expert
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

Please post a picture / screenshot of the two buttons. So I am sure that we speak about the same issue :-)

0 votes
Bharadwaj Jannu
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014
(function($) {
    /*
	AJS.toInit(function(){
        // old function
		alert("init");
    });
	*/
	
	JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context) {
        //alert("entered into greenhopper");
		AJS.$("#newOptions").click(function() {
			var tex1=AJS.$("#newField_customfield_12051");
			//alert(tex1);
			if(tex1 == null || tex1 == ''){
				alert("Inside 1");
			}
			else {
				//alert("In 2");
				var loopvar = 0;
				
				do {
					loopvar = setTimeout(temp, 1000);
					//alert(loopvar);
					//alert(typeof loopvar);
					
				} while (typeof loopvar != 'number');	
			}
			
		});
		
		AJS.$("#gh-create").click(function() {
			alert("on buttion clicked");
		});
		
		
		function temp() {
			var gps = grps();
			var cnt = 0;
			AJS.$("#newField_customfield_12051 option").each(function(i){
				var flag = false;
				
				var t1 = AJS.$(this).text();
				
				for (j = 0; j < gps.length; j++){
					var tem = gps[j].name;
					
					var conTerm = tem.toString();
					var flag1 = conTerm.match("NDA_" + t1 + "_[a-zA-Z]{2}[0-9]{6}");
					//alert(flag1);
					
					
					if(flag1 != null) {
						//alert(flag1);
						cnt = cnt + 1;
						flag = true;
						break;
					}
				}
				if(flag == false){
					AJS.$(this).remove();
				}
			});
			if(cnt == 0) {
				AJS.$("#newField_customfield_12051").parent().hide();
			}
		}
		function grps(){
			var groups;
			AJS.$.ajax({
				url: "/rest/api/2/user?username="+AJS.params.loggedInUser+"&expand=groups",
				type: 'get',
				dataType: 'json',
				async: false,
				success: function(data) {
					groups = data.groups.items;
				} 
			});
			//alert(groups);
			return groups;
		}
	});
	
})(AJS.$);

This is javascript placed as web-resource module.

when I click New Card button on Board, the script which I wrote is working fine. But when I press Create button on New Card pop-up the alert I placed is not getting executed.

0 votes
codelab expert
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

What do you mean with "new card context"? A new issue? Please post your code, so we can see what you want to do.

Suggest an answer

Log in or Sign up to answer