MultiSelect jquery Select Value

How can I select the values that are selected in this multiple select via jQuery?

<select id="gender" name="gender" multiple="multiple" style="width:90.4%; padding:5px; margin:5px; color:#000000; ">
    <option value="'M'">Male</option>
    <option value="'F'">Female</option>
</select>

Open in new window


I tried
gender: $("#gender").val()

Open in new window


Doesn't seem to work for some reason.
LVL 12
Nathan RileyFounderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nathan RileyFounderAuthor Commented:
Appears it is selecting my data but it's posting as an array versus just posting as 'M' or 'F'.  Anyway to stop it from creating an array?

The post value shows up like this when I vardump
array (size=1)
  0 => string ''M'' (length=3)

Not sure why it's turned into an array or adding additional quotes around it.
0
Julian HansenCommented:
Anyway to stop it from creating an array?
... as opposed to? It is a multi-select control - how else is it going to capture the multiple values selected.

The post value shows up like this when I vardump
Where is your post code?

How are you sending this through?

gender: $("#gender").val()

Open in new window

Does not really make sense - in the context of the usual definition of gender?

The code is doing what it is supposed to do - tell us what you want it to do and we can take it from there.
0
Julian HansenCommented:
It tried this code
$(function() {
  $('button').click(function() {
    $.ajax( {
      url: 't885.php',
      data: {
        gender: $('#gender').val()
      },
      type: 'POST',
      success: function(resp) {
        console.log(resp);
      }
    });
  });
});

Open in new window

Posted to t885.php
<?php
var_dump($_POST);
print_r($_POST);

Open in new window

Result:
array(1) {
  ["gender"]=>
  array(2) {
    [0]=>
    string(3) "'M'"
    [1]=>
    string(3) "'F'"
  }
}
Array
(
    [gender] => Array
        (
            [0] => 'M'
            [1] => 'F'
        )
)

Open in new window

Which is correct and exactly what would be expected.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Not sure why it's turned into an array or adding additional quotes around it.
It's an array because it has to be - it is a multiselect control - one name multiple values.
Regarding the quotes, look at the value's in your <option>'s
<option value="'M'">Male</option>
               ^ ^

Open in new window

You have single quotes as part of your option value - that is why the quotes are coming through to your values - because you put them there.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.