How to hide options from select list using JAVASCRIPT?

Here is a select list with A1, A2 and B1, B2 options. I want hide the A1 and A2 if the issue type is set to Bug during issue creation process.

In shrt, I have a drop down list and would like to remove an option from it, given the text/value of that particular option. Is it possible using javascript? Just like 'append' which adds an option to the drop down list, is there a function to remove an option?

I tried searching for it but all I got were examples where the entire set of options in the drop down list are removed, which is not what I seek.

4 answers

1 accepted

Yogesh, Following is the JS code for the later one.

<script type="text/javascript">
targetCategory = document.getElementById('customfield_10003'); // Main Option
targetSubCategory = document.getElementById('customfield_10003:1'); //Sub Category option.
subCategoryOptionsCount = document.getElementById('customfield_10003:1').length;
tempVar = 0;
while(tempVar < subCategoryOptionsCount)
	{
		if(targetSubCategory.options[tempVar].text=="Option's Name" )
		{
			targetSubCategory.remove(tempVar);
			subCategoryOptionsCount--;
		}	
		tempVar++;
	}
</script>

I guess the below code works for me. Even we can create Array for the base value and the field value which should be changed on the basis of base value.

<script language="JavaScript" type="text/javascript">
window.onload = function() {
var gwrCategory = document.getElementById('customfield_10100'); // MODIFY: Custom field ID
var undesiredValue = "Text option to be hidden"
var issueType = document.getElementById("issue-create-issue-type"); // MODIFY:
var issueA = issueType.innerHTML;
if(issueA == "Bug" ) {
for(var i=0; i<gwrCategory.options.length; i++){
if ( gwrCategory.options[i].text == undesiredValue ){
gwrCategory.remove(i);
break;
}
}
}
};
</script>

Hi Yogesh,

This will only help when the custom field is of Select List type. But what if the custom field is of Cascaded Style. Then there is a differnet way to hide the options in the cascaded list also.

Yes. I am aware of the option. Once I get some time, I will put the code. Why dont you put your option here itself?

Thanks Nikhil!!! It worked well!!!

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 Thursday in Marketplace Apps

Tips on how to choose the best estimation method for your planning

Planning and grooming sessions all come with their own sets of rules. Team members meet to estimate stories or other work items, all according to an agreed-upon process. And with every session comes ...

79 views 0 11
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