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

user2402179

I have the next selectbox in HTML:

<select id="listbox-taskStatus" class="selectpicker">
    <option status="0">In progress</option>
    <option status="1">Not started</option>
    <option status="2">Done</option>
    <option status="3">Failed</option>
</select>

I want to read the attribute value from selected option.

If just to take the value, it's simple:

var value = $('#listbox-taskStatus').val();

But what should I do, if I want to get the attribute value from selected option? I know, that .attr() must help, but when I tried to use it I've got the incorrect result.

I've tried the next:

var status = $('#listbox-taskStatus option').attr('status');

But the returned value is always 0 despite on fact I was selecting different option.

What's wrong, how to solve my problem?

Satpal

Use :selected Selector

var status = $('#listbox-taskStatus option:selected').attr('status');

However, I would recommend you to use data- prefixed attribute. Then you can use .data()

var status = $('#listbox-taskStatus option:selected').data('status');

DEMO

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JavaScript/jQuery - Get option attribute from select menu by text

From Dev

How to get all selected values from multiple select option?

From Dev

How to get the selected option value from select inside div

From Dev

How to get the value of option with attribute selected

From Java

jquery select change event get selected option

From Dev

Need to get selected option from Select component

From Dev

Get selected option value from specific select

From Dev

Need to get selected option from Select component

From Java

How to make the first option of <select> selected with jQuery

From Dev

Get Attribute Value From Specific Select Option

From Dev

How to get selected option in angular <select>

From Dev

How to get the option value of selected in Select dropdown

From Java

jQuery Get Selected Option From Dropdown

From Dev

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

From Java

Get custom attribute from selected option from datalist

From Dev

How to get selected option element attr in jQuery?

From Dev

How to get selected option element attr in jQuery?

From Dev

how to get the selected option id in jquery

From Dev

jQuery select2 - how to get property from option?

From Dev

Javascript get data attribute from selected option in a table

From Dev

How do I get all select elements that do not have an option selected using jQuery?

From Dev

How to set attribute selected true for option with no value in jquery

From Dev

jquery select change event when get selected option

From Dev

Get key value for selected option in select using jquery

From Dev

How can I exit from a jQuery event when an "unrelated" html select element has had no option selected?

From Dev

How do I fire a jQuery with an option selected from a Select2 dropdown?

From Dev

How can I disable selected attribute from select2() dropdown Jquery?

From Dev

How can I disable selected attribute from select2() dropdown Jquery?

From Dev

How to remove selected option from the option list in select2 multiselect and show selected options in the order they are selected

Related Related

  1. 1

    JavaScript/jQuery - Get option attribute from select menu by text

  2. 2

    How to get all selected values from multiple select option?

  3. 3

    How to get the selected option value from select inside div

  4. 4

    How to get the value of option with attribute selected

  5. 5

    jquery select change event get selected option

  6. 6

    Need to get selected option from Select component

  7. 7

    Get selected option value from specific select

  8. 8

    Need to get selected option from Select component

  9. 9

    How to make the first option of <select> selected with jQuery

  10. 10

    Get Attribute Value From Specific Select Option

  11. 11

    How to get selected option in angular <select>

  12. 12

    How to get the option value of selected in Select dropdown

  13. 13

    jQuery Get Selected Option From Dropdown

  14. 14

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

  15. 15

    Get custom attribute from selected option from datalist

  16. 16

    How to get selected option element attr in jQuery?

  17. 17

    How to get selected option element attr in jQuery?

  18. 18

    how to get the selected option id in jquery

  19. 19

    jQuery select2 - how to get property from option?

  20. 20

    Javascript get data attribute from selected option in a table

  21. 21

    How do I get all select elements that do not have an option selected using jQuery?

  22. 22

    How to set attribute selected true for option with no value in jquery

  23. 23

    jquery select change event when get selected option

  24. 24

    Get key value for selected option in select using jquery

  25. 25

    How can I exit from a jQuery event when an "unrelated" html select element has had no option selected?

  26. 26

    How do I fire a jQuery with an option selected from a Select2 dropdown?

  27. 27

    How can I disable selected attribute from select2() dropdown Jquery?

  28. 28

    How can I disable selected attribute from select2() dropdown Jquery?

  29. 29

    How to remove selected option from the option list in select2 multiselect and show selected options in the order they are selected

HotTag

Archive