0

i've the following select box

<select class="selectpicker display-block tax main-tax" name="my_selectbox_name" multiple="" data-none-selected-text="mytext" >
<option value="my_option_value" data-myid="123">my_text</option>
...
</select>

I access to the value in this way

response.my_selectbox_name = $('.main select.tax').selectpicker('val');

How i can access the data-myid value?

2

2 Answers 2

1

Jquery has a .data() method built in for either getting or setting values for a data attribute. When this method is provided with a key like .data(key) it will return the value stored in the data attribute. So for your particular example it would be .data('myid'). For data attributes anything after 'data-' is considered the key. So for your <select data-none-selected-text="mytext"> the key is 'none-selected-text'

But to answer your direct question it looks like this:

$('.selectpicker option').data('myid')

Sign up to request clarification or add additional context in comments.

1 Comment

possible selected... $('.selectpicker option:selected').data('myid')
0

There are several ways.

1. the way to get value
$("option").attr("data-myid"); // 123
$("option").dataset.myid; // 123
2. the way to set value
$("option").attr("data-myid", "YOUR_VALUE"); // YOUR_VALUE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.