Herman Ceaser
Herman Ceaser

Reputation: 90

pushing selected options From multiple select tags into an array

Iam getting selected option values from Multiple selects and i want to push them into an array

This is the Jquery Code ive written to get a selected option whenever the select changes and push the value onto an array

/Admission.js

var myGrades = [];
$('.uace_grade').each(function(){
    $(this).change(function(){
        var $option = $(this).find(':selected').val();
        myGrades.push($option);
    });
    $('#result').html(myGrades);
});

And The Html Looks Like this

<th class="textbold">Grade</th>                                              
<td>
    <select name="grade1" class="form-control textbox required uace_grade">
        <option value="">Grade</option>
        <option value="6">A</option>
    </select>
</td>
<td>
    <select name="grade1" class="form-control textbox required uace_grade">
        <option value="">Grade</option>
        <option value="5">B</option>
    </select>
</td>
<td>
    <select name="grade1" class="form-control textbox required uace_grade">
        <option value="">Grade</option>
        <option value="4">C</option>
    </select>
</td>
<td>
    <select name="grade1" class="form-control textbox required uace_grade">
        <option value="">Grade</option>
        <option value="3">D</option>
    </select>
</td>
<td>
    <select name="grade1" class="form-control textbox required uace_grade">
        <option value="">Grade</option>
        <option value="2">E</option>
    </select>
</td>
<td>
    <select name="grade1" class="form-control textbox required uace_grade">
        <option value="">Grade</option>
        <option value="1">O</option>
    </select>
</td>

I expected To write the Array results in a table cell with id of #result but nothing is showing, i know i could have missed something

Upvotes: 0

Views: 1005

Answers (1)

D. Islam
D. Islam

Reputation: 66

$('#result').html(myGrades); loop through the line of code.

you can use the following.

$('#result').html('');
for(var i=0;i<myGrades.length;i++){
   $('#result').append(myGrades[i]);

}

Upvotes: 1

Related Questions