0

i am having the results as following. (id & value)

{"5702":"7am - 10am","5703":"10am - 1pm","5704":"12pm - 3pm","5705":"3pm - 6pm","5706":"6pm - 9pm","5707":"7pm - 10pm","5708":"9pm - 12am","5709":"12am - 7am","5730":"AZRAAR 123"}

This is taken as below.

JSON.stringify(<?php echo $timeWindowJson ?>)

Now i am having this append dummy drop down.

var array = selectedDates.split(",");
      var dynamicTable = "<table id='tbl_dynamic_call_dates'><tr><td>Appointment date</td><td>Client time window</td>";
      $.each(array, function(i) {
         dynamicTable += "<tr><td>" + array[i] + "</td><td>" + "<select><option value='volvo'>7am - 10am</option></select>" + "</td></tr>";
      });
      dynamicTable += "</table>";
      $("#div_outgoing_call_dates_rows").append(dynamicTable);

but i want the dropdown appended to be like this.

<select name="CSSAtapsClient[gender_type_id]" class="long" id="gender_type_id">
<option value="">- - Select Gender - -</option>
<option value="5702">7am - 10am</option>
<option value="5703">10am - 1pm</option>
<option value="5704">12pm - 3pm</option>

...... (till all the values are added from json string)

3 Answers 3

3

Wait a minute the string you have provided is not JSON.... you may correct it first...

Sample JSON STRING SHOULD LOOK LIKE THIS

[{"id":"1063","dewr_code":"Unknown Division - ML & Psych"},{"id":"1066","dewr_code":"YorkePDGP - Dale"}]

You may try something like this inside the AJAX call.....

success: function(data) {
            $('#state_id').html('').append('<option value="">- - Select State - -</option>');
            $.each(data, function(i, value) {
               $('#state_id').append('<option value="' + value.id + '">' + value.state + '</option>');
            });
            $('#state_id').trigger('change');
         }
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need to change the structure of the data. It's an object, and each can loop happily through an object.
1

You can give this a try if your shown data is not a proper json (as it dont look like json),

var array = selectedDates.replace("{","").replace("}","").split(",");
var selectHTML = "<select><option value=''>--Select--</option>";
$.each(array, function(i,value) {
    selectHTML += "<option value='"+value.split("-")[0]+"'>"+value.split("-")[1]+"</option>";
});
selectHTML += "</select>";
var dynamicTable = "<table id='tbl_dynamic_call_dates'><tr><td>Appointment date</td><td>Client time window</td>";
$.each(array, function(i,value) {
 dynamicTable += "<tr><td>" + value.split("-")[0] + "</td><td>" + selectHTML + "</td></tr>";
});
dynamicTable += "</table>";
$("#div_outgoing_call_dates_rows").append(dynamicTable);

This should work for you.

Comments

1

In your HTML:

<select id="gender_type_id">
  <option value="">- - Select Gender - -</option>
</select>

In your Javascript:

$.each(obj, function(key, value) {
  $('#gender_type_id').append('<option value="' + key + '">' + value + '</option>');
});

The thing you're calling an array isn't an array. It's just a JS object with a number of properties. Using each is fine, but you need to pass the key and value through rather than the element and index.

See it working here.

Comments

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.