Get selected values ​​and radio values ​​using jquery

advertisements

I have the following set of html for both radio and select box i want to get the value with comma seperated

FOR RADIO BUTTON

<div class="element" id="radio_gender">
    <div class="fleft">
        <label for="gender">Gender</label>
    </div>
    <div class="fleft">
        <input type="radio" value="Male" name="gender"  />Male
        <br />
       <input type="radio" value=" Female" name="gender"  /> Female<br />
   </div>
   <div class="clr"></div>
</div>

FOR SELECTBOX

<div class="element" id="select_gender2">
     <div class="fleft">
        <label for="gender2">Gender2</label>
     </div>
     <div class="fleft">
         <select name="gender2" id="gender2" >
             <option value="Male">Male</option>
             <option value=" Female"> Female</option>
         </select>
     </div>
     <div class="clr"></div>
</div>

for bothe the above case i need a string like Male, Female


For the radio button:

var values = $("input:radio[name='gender']").val(); // selects all radios with name
                                                    // gender and gets their values
var string = values.join(",");

For the select box:

var values = $("#gender2 option").val(); // finds all option tags under id
                                                 // gender2
var string = values.join(",");

EDIT: Given the below comments (which check out against the jquery docs, sorry about the initial mistake), try -

var values = [];
$("input:radio[name='gender']").each(function() {
    values.push($(this).val());
});
var string = values.join(",");

and

var values = [];
$("#gender2 option").each(function() {
    values.push($(this).val());
});
var string = values.join(",");