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

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

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

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

But I require each time I press Create button.

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

(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.

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

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");
        });

});

The above mentioned scripts are not working.

Ok, what about this:

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

    alert("DOM changed");

});

Hm, I am out of ideas...

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

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

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

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

even the above is not showing any alert.

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

})

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
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
Published May 21, 2018 in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

1,224 views 10 18
Read article

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