Skip to main content

I need to set by JavaScript a value to a dropdown field control knowing only Text.

For example, There is a dropdown control that has 3 values - One, Two, Three. I need to set my value to Three by JavaScript. I found that aricle Custom Script on O365 and according to this I can set value only knowing Id, but I want set value not by Id but by Text.

 

That works:

  1. NWF.FormFiller.Events.RegisterAfterReady(function () {  
  2.     NWF$('#' + ddlStaff).on('change'function (e) {  
  3.         if (e.originalEvent == undefined) {  
  4.             if (this.value == "") {  
  5.                 this.value = "1";  
  6.             }  
  7.         }  
  8.     });  
  9. });

 

But like this it does not work:

  1. NWF.FormFiller.Events.RegisterAfterReady(function () {  
  2.     NWF$('#' + ddlStaff).on('change'function (e) {  
  3.         if (e.originalEvent == undefined) {  
  4.             if (this.value == "") {  
  5.                 this.value = "Three";  
  6.             }  
  7.         }  
  8.     });  
  9. });

Any ideas how to set value by Text?

I have found the solution:

NWF$("#" + ddlStaff + " option:contains(" + Three +")").attr("selected", true);  


Reply