jquery code to remove / add options to dropdownlist (choice field)

  • 19 July 2017
  • 3 replies
  • 98 views

Badge +4

Hi Team,

I have been implemented the code to remove Options from dropdownlist using the following code.

foo = "0 - Not applicable";
NWF$("#" + LossStatus + " option:contains(" + foo +")").remove();
foo = "1 - Loss not yet closed";
NWF$("#" + LossStatus + " option:contains(" + foo +")").remove();
foo = "2 - Loss closed";
NWF$("#" + LossStatus + " option:contains(" + foo +")").remove();

after that base on my condition i would like to add options to my dropdownlist with the following code.

NWF$('#' +LossStatus).append($('<option>', {value: 1,text: '1 - Loss not yet closed'}));

How ever its showing in dropdown. but could not able to save the form.

Showing error as "Some thing went wrong" and some correlation id.

Please help us to the correct way to add options to dropdown,

Regards,

Dhayanand


3 replies

Userlevel 6
Badge +15

Hi there - 

So from looking at what you have here, I would say that you'd want a list with your dropdown items - and then a column with something that will allow you to filter on it to hide / show things as required.

For example - the column could be "manager" with "yes" and "no" for each item - as in, can a manager view this? Then, you could use a ListLookup and filter on the field "Manager" - controlled by a single line of text in your form (that you'll hide) and update based on whatever you need to filter with. Like, "if the current user is a manager, then this will be YES... if not, then NO." That kind of thing.

So in your case ... if control is equal to 0 not applicable, then filter using "not applicable", otherwise, use "applicable."

Badge +4

removing option from dropdown list

foo = "0 - Not applicable";
NWF$("#" + LossStatus + " option:contains(" + foo +")").remove();

adding option to dropdown list

NWF$('#' +LossStatus).append($('<option>', {
value: '1 - Loss not yet closed',
text: '1 - Loss not yet closed'
}));

Removing duplicate option values in dropdown

var usednames={};
$("select[id="+LossStatus+"]>option").each(function(){
if(usednames[this.text]){
$(this).remove();
}
else{
usednames[this.text]=this.value;
}
});

Badge +4

removing option from dropdown list

foo = "0 - Not applicable";
NWF$("#" + LossStatus + " option:contains(" + foo +")").remove();

 

adding option to dropdown list

NWF$('#' +LossStatus).append($('<option>', {
value: '1 - Loss not yet closed',
text: '1 - Loss not yet closed'
}));

 

Removing duplicate option values in dropdown

var usednames={};
$("select[id="+LossStatus+"]>option").each(function(){
if(usednames[this.text]){
$(this).remove();
}
else{
usednames[this.text]=this.value;
}
});

Reply