Search code examples
jsonjquerygetjson

Send list of :selected from multiple selectable select via getJSON


I have a list of dates in a multiple select elemental, and I need to send the list of selected dates to server for handling.
The jQuery is going haywire giving me a continuous increase in both memory and cpu usage, which I dont understand as running the haywire part alone is fine.

HTML
<select id="my-dates" name="my-dates[]" multiple="multiple" size="5">
  <option>2011-01-18</option>
  <option>2011-01-20</option>
  <option>2011-01-21</option>
  <option>2011-01-27</option>
</select>

jQuery
$.getJSON('dates_handling.php',{
 dates: $('select#my-dates').find(':selected'),
 other:stuff
},function(result){
  // result handling
});
data key 'dates' should contain an transmit-able array like ['2011-01-20',2011-01-27']

jQuery haywire part
$('select#my-dates').find(':selected')

Solution

  • I'd build the date array manually because you are passing full jQuery objects:

    var selectedDates = [];
    
    $('select#my-dates > option:selected').each(function() {
        selectedDates.push($(this).html());
    });
    
    $.getJSON('dates_handling.php',{
     dates: selectedDates,
     other:stuff
    },function(result){
      // result handling
    });