0

I want to push the options from a dropdown to the nested array "data".

var gnames = { data: [], list: { sort: { enabled: true } } };
$("#dropdown-group option.lgroup").each(function(){
  gnames.data.push($(this).val());
});
$("#save-group").easyAutocomplete(gnames); 

Why does it not work?

5
  • 2
    In what way is it not working? What happens in your code? Commented Oct 25, 2019 at 8:18
  • post code on fiddle, that we can identify what the actually problem Commented Oct 25, 2019 at 8:23
  • @chandukomati better to recommend a snippet within SO than a fiddle as the fiddle site may go down in future. Commented Oct 25, 2019 at 9:22
  • You'll need to include (some of) the HTML for the select - are you sure each of your options has class=lgroup ? Or does the select have that class and that's your problem? We also don't know that your select id=dropdown-group (or the parent div could also have that id) Commented Oct 25, 2019 at 9:24
  • 1
    @freedomn-m there you go, I forgot to set lgroup class Commented Oct 25, 2019 at 9:33

1 Answer 1

1

var result = {
  data: []
};
$('button').on('click', function() {
  $('#select').find('option').each(function() {
    result.data.push($(this).val());
  })
  console.log(result);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="select">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select>
<button>Set</button>

My code can work.

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

2 Comments

"works for me" is a comment at best, it's not an answer. OP:"Why doesn't it work" A:"it does" OP:oh, ok then
My code worked if I didnt forget to set class lgroup :))

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.