0

I'm very new to JQuery and I try to fill my html select boxes with Jquery but they stay empty. Below is my code:

   $('select').each(function(){
      var action = 'SELECT_FLDS_GRID';
      var fldnam = $(this).attr('name');
      $.getJSON('frm.grid.php',{'action':action,'fldnam':fldnam},function(j){
         var_SelectOption(j,fldnam);
      });
   });
   function var_SelectOption(j,myfld)
   {
      var options = '';
      for(var i=0;i<j.length;i++)
      {
         options += '<option value="' + j[i].option + '">' + j[i].option + '</option>';
      }
      $('select').each(function(){
         if($(this).attr('name') == myfld) {$(this).html(options);}
      });
   } 

For clarification, if I change var_SelectOption with below code I do get a result:

   function var_SelectOption(j,myfld)
   {
      var options = '';
      for(var i=0;i<j.length;i++)
      {
         options += '<option value="' + j[i].option + '">' + j[i].option + '</option>';
      }
      $('select').html(options);
   } 

However that's of course not the intention as this just puts the same optionlist to each select box. So this is just to show where the problem is; the optionlist strings would need to get attached to the proper select element.

I've been playing with it for quite some time but can't find the issue. I'm even wondering if I'm doing it completely wrong... Hopefully somebody can help me here.

regards, Patrick

1 Answer 1

2

Try $('select[name="'+myfld+'"]').html(options);

Sign up to request clarification or add additional context in comments.

1 Comment

Hey thanks for the quick answer and yes that works! I tried this before (and also in several other ways) but always with @name and no quotes, because that's how I read it in the online docs(docs.jquery.com/DOM/Traversing/Selectors): $("input[@name=bar]").val(); Not sure what the difference is but it works perfect as you answered. Thanks a lot. Patrick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.