1

I'm looking to return a CSV formatted string to set the value of a input text field with the OPTION values from a SELECT box.

$.map($('#addedchargeamtid option'), function(e) { var exp = $(e).text(); alert(exp); })

In using a map function but it looks to overwrite the value each time. can't figure out how to concat these together somehow.

BTW thanks to all the jQuery Gurus today, much props on my issues!!!

UPDATING:

<option value="123">123</option>
<option value="asd">asd</option>
<option value="trr">trr</option>
<option value="345">345</option>

I need this:

123,asd,trr,345

But the options are dynamic and can be added or removed, so there could be 1 or 100

NEW:

Well this is kinda working. it gives me 4 options of the same item when I only added it once. also does not update the hidden text field with the CSV value

// Add remove button
$('#addButton').click(function() {
   $('#removeButton').show();

   // Add 
   var myOptions = $('#typedchargeamtid').val();
   $.each(myOptions, function() {
      $('#addedchargeamtid').append(
         $('<option></option>').val(myOptions).html(myOptions)
      );
   });

   var txt = $('#addedchargeamtid').val() || [];
      $('#commasepchargeamtid').val(txt.join(','));
   });

Thanks again

Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
  • so what you want is a textbox populated with the values from the dropdown list? with a comma separating each value? – TStamper May 18 '09 at 23:40
  • yes. This is the second part of my other question. http://stackoverflow.com/questions/879842/jquery-enable-disable-show-hide-button-w-select-options-get-remaining-option-va – Phill Pafford May 18 '09 at 23:42
  • I'm assuming you can just put it in a each() method and store each value separately,but not sure if that would work – TStamper May 18 '09 at 23:52
  • Not to sound rude, but have you checked the variables being used during that Add block? It looks like you are each-ing the myOptions array and appending to a different select block the entire myOptions array itself. I think you may have missed the (index,value) arguments to the anonymous funciton for the $.each loop. See http://docs.jquery.com/Utilities/jQuery.each – Danny May 19 '09 at 00:41
  • you are not rude and thanks for helping me understand more about jQuery. what would be a better way to do this? – Phill Pafford May 19 '09 at 04:09

2 Answers2

6

You can use each instead of map:

    var options = Array();
    $('#addedchargeamtid option').each(function(index){
        options[index] = $(this).val();
    });

    $('#commasepchargeamtid').val(options.join(','));
Nadia Alramli
  • 111,714
  • 37
  • 173
  • 152
0

The JQuery documentation states that the $.val() function when used on a select will return a list of selected values. Just simply using that with an array.join should accomplish what you are looking for I believe.

var txt = $('#addedchargeamtid option').val() || [];
$('#myinputbox').val(txt.join(','));

Or there is an array.reduce function in Javascript 1.8

var txt = $('#addedchargeamtid option').val() || [];
$('#myinputbox').val(txt.reduce(
   function(prev, next, index, array){
       return prev + "," + next;
   },
   '')
);
Danny
  • 13,194
  • 4
  • 31
  • 36