It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Bharadwaj Jannu 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 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 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
codelab expert 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.

0 votes
Bharadwaj Jannu 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 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
codelab expert 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 May 12, 2014

The above mentioned scripts are not working.

codelab expert May 12, 2014

Ok, what about this:

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

    alert("DOM changed");

});

codelab expert May 12, 2014

Hm, I am out of ideas...

Bharadwaj Jannu 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 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 May 12, 2014

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

codelab expert May 12, 2014

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

Bharadwaj Jannu May 12, 2014

even the above is not showing any alert.

codelab expert 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
Bharadwaj Jannu May 12, 2014

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

The above is New Card button on Board.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Jira Software

How to prevent the propagation of unused project schemes, workflows & screens in Jira software

Atlassian ranks project attributes as the third most important factor impacting performance in the category of data. It’s not surprising, since project attributes are precisely the rules used to ma...

1,371 views 1 17
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you