1

I have a form where you select a location, this location has a zip code tied to it and is captured in the data-foo value. What I need is an array built upon multiple locations being selected.

An example would be if both would be selected I'd have 65807 => 71118

Form:

<form enctype='multipart/form-data' role='form' action='' method='post'>	
		<div class="row">
			<div class="col-md-3">
				<div class='form-group'>							
					<label for='select'>Destination(s)&nbsp;</label>
						<select name='destination[]' style='height: 200px;' multiple class='form-control'  multiple='multiple' id='destination' style='lane'>";
							<option value='Springfield' data-foo='65807'>Springfield, MO</option>
							<option value='Shreveport' data-foo='71118'>Shreveport, LA</option>
						</select>
				</div>
			</div>
		</div>
</form>

What I have so far for JS:

$(function(){
    $('#origin').change(function(){
        var selected = $(this).find('option:selected');
       $('#origin_zip').html(selected.data('foo')); 
    }).change();
});

$('#destination').change(function() {
    $('#destination_zip').text('');
    
    var selected = $('#destination').val();
    for (var i = 0; i < selected.data; i++) {
       $('#destination_zip').data($('#destination_zip').data('data-foo') + selected[i]);
    }
});

EDIT:

This is the code that works on building the array with the text, but not the data-foo that I need.

$('#destination').change(function() {
    $('#destination_zip').text('');
    
    var selected = $('#destination').val();
    for (var i = 0; i < selected.length; i++) {
       $('#destination_zip').text($('#destination_zip').text() + selected[i]);
    }
});

5
  • So you want the user to be able to select multiple locations? A Select Option menu only allows ONE option to be selected. I think you want check boxes or radio buttons. And the example you gave is in the form a key value which isn't available in javascript. You could build a json object that way though. Commented Feb 6, 2015 at 22:48
  • I can have it build an array based upon .text using .val() already.. I just need to change the .text to a form of .data Commented Feb 6, 2015 at 22:50
  • Not sure what you want to do here... what is the difference between .text and .data? Commented Feb 6, 2015 at 22:52
  • I've updated original post with the code that works for the text only. Commented Feb 6, 2015 at 22:52
  • To TRGWII - I need to pull the zip code which is held in data-foo, but have the user select by City, St. Commented Feb 6, 2015 at 22:53

2 Answers 2

1

The following could be used:

$('#destination').change(function() { // Whenever the select is changed
    var arr = []; // Create an array
    $('#destination option:selected').each(function(){ // For each selected location
        arr.push($(this).data("foo")); // Push its ZIP to the array
    });
    console.log(arr); // will include the ZIP code(s) of selected location(s).
});

jsFiddle example here

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

Comments

0

Something like this? (kind of ugly, I know)

$('#destination').change(function() {
  var selected = [];
  for(i = 0; i < $('#destination').children().length; i++){
    selected[i] = $($('#destination').children()[i]).data('foo');
  }
  console.log(selected);
});

Edit: nevermind, look at @dsg's answer

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.