Save textbox to array

by Rey Den Nalasa

HTML

<div class="schedule_container">
  <div class="col_1_schedule">
    <input type="checkbox" name="open_days[]" class='open_days' value="Sunday">Sunday </div>
  <div class="col_2_schedule">
    <h4>Morning Session</h4>
    <input type="text" name="0_start_am" class="am_picker ui-timepicker-input" autocomplete="off">
    <input type="text" name="0_end_am" class="pm_picker ui-timepicker-input" autocomplete="off">
  </div>
  <div class="col_3_schedule">
    <h4>Afternoon Session</h4>
    <input type="text" name="0_start_pm" class="am_picker ui-timepicker-input" autocomplete="off">
    <input type="text" name="0_end_pm" class="pm_picker ui-timepicker-input" autocomplete="off">
  </div>
  <div class="col_4_schedule">
    <h4>Evening Session</h4>
    <input type="text" name="0_start_evening" class="am_picker ui-timepicker-input" autocomplete="off">
    <input type="text" name="0_end_evening" class="pm_picker ui-timepicker-input" autocomplete="off">
  </div>
  <div style="clear: both;"></div>
  <div class="col_1_schedule">
    <input type="checkbox" name="open_days[]" class='open_days' value="Monday">Monday </div>
  <div class="col_2_schedule">
    <h4>Morning Session</h4>
    <input type="text" name="1_start_am" class="am_picker ui-timepicker-input" autocomplete="off">
    <input type="text" name="1_end_am" class="pm_picker ui-timepicker-input" autocomplete="off">
  </div>
  <div class="col_3_schedule">
    <h4>Afternoon Session</h4>
    <input type="text" name="1_start_pm" class="am_picker ui-timepicker-input" autocomplete="off">
    <input type="text" name="1_end_pm" class="pm_picker ui-timepicker-input" autocomplete="off">
  </div>
  <div class="col_4_schedule">
    <h4>Evening Session</h4>
    <input type="text" name="1_start_evening" class="am_picker ui-timepicker-input" autocomplete="off">
    <input type="text" name="1_end_evening" class="pm_picker ui-timepicker-input" autocomplete="off">
  </div>
  <div style="clear: both;"></div>
</div>


<input type="submit"...

JavaScript

$("input[name=submit]").click(function() {
  var schedules = $('.open_days:checkbox:checked').map(function() {
    return this.value;
  }).get();
  
  //console.log(schedules);

  var schedule_container = {};
  
  $.each(schedules, function(index, value) {
  
  	alert(index+" "+value);
    
    var time = {};
    //morning
    time[ index + '_start_am'] = $('input[name=' + index + '_start_am]').val();
    time[ index + '_end_am'] = $('input[name=' + index + '_end_am]').val();
    //afternoon
    time[ index + '_start_pm'] = $('input[name=' + index + '_start_pm]').val();
    time[ index + '_end_pm'] = $('input[name=' + index + '_end_pm]').val();
    //evening
    time[ index + '_start_evening'] = $('input[name=' + index + '_start_evening]').val();
    time[ index + '_end_evening'] = $('input[name=' + index + '_end_evening]').val();
    
    schedule_container[value] = time;
    
    console.log(schedule_container);
    
  });
});