0

I want to create a select box dropdown. initially select box will not have any option, it should show only select. On click of select box it will iterate list and add each option to that select box.

I am trying below code -

$(".droptest").click(function(e){
e.preventDefault();

            var listDrop = [{"chdk74":"hdk73","hiphdk":"hiphdk","hpghdk":"hpghdk","ihdk":"ihdk","sdg74":"sdg74"}];
            var dropDownlist = JSON.parse(listDrop);
            var options = "";

            $.each(dropDownlist, function(key,value) {
                options += "<option name=" + value +" id=" + value +" value="+value+"> "+ value +" </option>";
            });
            $(".DomainDrop").html(options);
        });

<div class="droptest">

        <select class="DomainDrop tet" id="DomainDropCreate" name="DomainDrop" required>
            <option name="" value="">Select Domain</option>

        </select>
    </div>
1
  • Please see your browser console for your error message. JSON is a string representation of a javascript object. You have a javascript object, already, so you can't parse it. So it's failing at that step. Commented Nov 26, 2019 at 9:59

1 Answer 1

2

What you would do is to remove the [] from listDrop, and then use $.each(listDrop, function(key, value) {

Demo

$(".droptest").mousedown(function(e) {

  var listDrop = {
    "chdk74": "hdk73",
    "hiphdk": "hiphdk",
    "hpghdk": "hpghdk",
    "ihdk": "ihdk",
    "sdg74": "sdg74",
  };
  var options = "";

  $.each(listDrop, function(key, value) {
    options += "<option name=" + value + " id=" + value + " value=" + value + "> " + value + " </option>";
  });
  if ($(".DomainDrop option").length < 2) {
    $(".DomainDrop").html(options);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="droptest">

  <select class="DomainDrop tet" id="DomainDropCreate" name="DomainDrop" required>
    <option name="" value="">Select Domain</option>
  </select>
</div>

As Freedomn-m says, change click to mousedown and remove e.PreventDefault() then I all works just fine.

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

8 Comments

hi, i checked. i is not selecting after click. like if i select any option it is showing ihdk74 that means first only.
why list is not poping up on first click, on second click it is coming to select?
@codegeek Because when you click it the first time and the dropdown is already open before you populate it. Why don't you not just populate the select on page load?
Change the first line to $("#DomainDropCreate").mousedown(function(e) { - the issue with click on the div is that it only gets there because it bubbled up from the select so the select click event has already happened. Mouse down on the select occurs before the click on the select so you can change the html/options before it is displayed. You might also prefer $("'id").one("mousedown", ... so it only reloads once.
@freedomn-m You are correct, I've also removed e.preventDefault() since that will not allow the dropdown to open.
|

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.