You are viewing limited content. For full access, please sign in.

Discussion

Discussion

Manually Add Options to Drop Down List using Jquery

posted on September 21, 2016

I am needing to retrieve the value from a drop down after a lookup is performed. Once I retrieve the value, I would like to append additional options to the drop down list based on the lookup value that was inserted into the drop down field. 

This will allow the user to then change the selected option within the drop down to another option I appended. 

I am also needing to do this for every drop down list that has the class ".dropdown"

Any assistance would be great!

0 0
replied on September 21, 2016

I was able to resolve the issue. 

Here is the script below:

$('document').ready(function(){
//Create Counter Variables
var index = 0;

  function wait(ms) {
    var deferred = $.Deferred();
    setTimeout(deferred.resolve, ms);
    return deferred.promise();
  }
  
  var promiseOne;
  
  $('.empid input').focusout(function(){
    
    promiseOne = wait(1000);

    promiseOne.then(function(){
      
      $('.dropdown select').each(function(){
      
        if($(this).val() == 'Y') {var newOption = "<option value='N'>N</option>";
            					  $(this).append(newOption);
        } else { 
              					  var newOption = "<option value='Y'>Y</option>";
                                  $(this).append(newOption); }
        
      });    
    
  });
 
  });

});

 

0 0
replied on September 21, 2016 Show version history

I would also like to note that I am trying to stay away from selector IDs that are unique to each individual drop down field. The reason for this is to allow the customer to just apply the dropDown class to the drop down field. It will then allow the field to be editable, keeping the original lookup value selected and the appended values I added to the list. 

('document').ready(function(){

$('.dropDown').on('change lookup',function(){
 
var newOption = "<option value='No'>No</option>

if($('.dropDown select').val() == 'Y') {

var newOption = "<option value='N'>No</option>

} else {  

var newOption = "<option value='Y'>Yes</option>

}

$('.dropDown select').append(newOption);

});

});

The problem with this is that each time a lookup change is performed on a drop down with the "dropDown" class, it appends another value creating a list of "No"s or "yes"s. 

As well, each time you change the value, it appends another value. 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.