Get selected option value from specific select

Shariati

I can not get the selected <option> value. <select>s are formed dynamically from database into table rows. All <select>s have same class name.

Here is my HTML:

<tr>
    <td class="std_code">2014-1-10</td>
    <td>Student 10</td>
    <td>66</td>
    <td>100</td>
    <td>66</td>
    <td>
        <select class="degree_accept_id">
            <option value="1">BA(LAW)</option>
            <option value="2">BE(EE)</option>
        </select>
    </td>
    <td>17 November 2014</td>
    <td>
        <a href="" class="ee_accept_suggestion"><i class="fa fa-check fa-lg"></i></a>
    </td>  
</tr>
<tr>
    <td class="std_code">2014-1-9</td>
    <td>Student 9</td>
    <td>69</td>
    <td>100</td>
    <td>69</td>
    <td>
        <select class="degree_accept_id">
            <option value="1">BA(LAW)</option>
            <option value="2">BE(EE)</option>
        </select>
    </td>
    <td>17 November 2014</td>
    <td>
        <a href="" class="ee_accept_suggestion"><i class="fa fa-check fa-lg"></i></a>
    </td>  
</tr>
<tr>
    <td class="std_code">2014-1-8</td>
    <td>Student 8</td>
    <td>71</td>
    <td>100</td>
    <td>71</td>
    <td>
        <select class="degree_accept_id">
            <option value="1">BA(LAW)</option>
            <option value="2">BE(EE)</option>
            <option value="3">BE(ME)</option>
        </select>
    </td>
    <td>17 November 2014</td>
    <td>
        <a href="" class="ee_accept_suggestion"><i class="fa fa-check fa-lg"></i></a>
    </td>  
</tr>

JS code:

<script>
    $(document).ready(function() {
        $('.ee_accept_suggestion').click(function(e) {
            e.preventDefault();
            var std_code = $(this).parent().siblings(".std_code").text();
            //var selected_degree = $(".degree_accept_id option:selected", this).attr("value");
            //var selected_degree = $(this).val();
            var selected_degree = $("select.degree_accept_id").val();
            alert(selected_degree);
        });
    });
</script>

The problem: alert always shows 1.

Travis J

If you want the selected value of the dropdown relative to the click event, use this

$('.ee_accept_suggestion').click(function(e){
 var dropdownValue = $(this).closest('tr').find('.degree_accept_id').val();

What is happening is

  • First, the clicked element is used $(this) and is wrapped in a jQuery object to have access to the API,
  • .closest('tr') searches itself first, and then for the first occurrence of parent nodes which are <tr>.
  • Once the <tr> element is found, .find('.degree_accept_id') will locate the element under that tr classed as "degree_accept_id".
  • jQuery's .val() gets the value associated with an input, select (in this case), or textarea element.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get Attribute Value From Specific Select Option

From Dev

How to get the selected option value from select inside div

From Dev

How to get specific value from option select - json object

From Dev

How to get the option value of selected in Select dropdown

From Dev

Need to get selected option from Select component

From Dev

Need to get selected option from Select component

From Dev

Render select Tag from jQuery Ajax , then get the value from selected option

From Dev

Get Selected Option by Value

From Dev

Select specific <option> from <select> while having a value from PHP

From Dev

get the value and status (selected or not) of the option that triggers a multiple select onchange event

From Dev

Get key value for selected option in select using jquery

From Dev

javascript - get selected value from select list

From Java

Set select option 'selected', by value

From Dev

Change value of a input (write into the input) when a option is selected from a select

From Dev

Populate Selected option in options_for_select with boolean value from database

From Dev

Get selected option from select with angular JS and ionic

From Dev

How to get attribute from selected option of select in jQuery?

From Dev

jsoup - get selected option from html select's options

From Dev

How to get all selected values from multiple select option?

From Dev

Get optGroup id from selected option in select2

From Dev

Get selected option from the tag <select> in php to use on if

From Dev

How to get the value of option selected from JOptionPane.showMessageDialog?

From Dev

get selected value from option tag and store in db

From Dev

Don't allow to select other option if selected specific value with select2

From Dev

Is it possible to target a <select> with a specific option selected?

From Dev

Alert or message on specific option selected in select menu

From Dev

Get the text from a selected option

From Dev

Get text from selected option

From Java

How to revert back Mat-Select's selected value for a specific option?

Related Related

  1. 1

    Get Attribute Value From Specific Select Option

  2. 2

    How to get the selected option value from select inside div

  3. 3

    How to get specific value from option select - json object

  4. 4

    How to get the option value of selected in Select dropdown

  5. 5

    Need to get selected option from Select component

  6. 6

    Need to get selected option from Select component

  7. 7

    Render select Tag from jQuery Ajax , then get the value from selected option

  8. 8

    Get Selected Option by Value

  9. 9

    Select specific <option> from <select> while having a value from PHP

  10. 10

    get the value and status (selected or not) of the option that triggers a multiple select onchange event

  11. 11

    Get key value for selected option in select using jquery

  12. 12

    javascript - get selected value from select list

  13. 13

    Set select option 'selected', by value

  14. 14

    Change value of a input (write into the input) when a option is selected from a select

  15. 15

    Populate Selected option in options_for_select with boolean value from database

  16. 16

    Get selected option from select with angular JS and ionic

  17. 17

    How to get attribute from selected option of select in jQuery?

  18. 18

    jsoup - get selected option from html select's options

  19. 19

    How to get all selected values from multiple select option?

  20. 20

    Get optGroup id from selected option in select2

  21. 21

    Get selected option from the tag <select> in php to use on if

  22. 22

    How to get the value of option selected from JOptionPane.showMessageDialog?

  23. 23

    get selected value from option tag and store in db

  24. 24

    Don't allow to select other option if selected specific value with select2

  25. 25

    Is it possible to target a <select> with a specific option selected?

  26. 26

    Alert or message on specific option selected in select menu

  27. 27

    Get the text from a selected option

  28. 28

    Get text from selected option

  29. 29

    How to revert back Mat-Select's selected value for a specific option?

HotTag

Archive