Jump to content

javascript select option by data-value


nitiphone2021
Go to solution Solved by requinix,

Recommended Posts

<div class="choices__list" role="listbox"><div id="choices--p_nationality-do-item-choice-1" class="choices__item choices__item--choice choices__placeholder choices__item--selectable" role="option" data-choice="" data-id="1" data-value="" data-select-text="ກົດເລືອກ" data-choice-selectable=""></div><div id="choices--p_nationality-do-item-choice-2" class="choices__item choices__item--choice is-selected choices__item--selectable is-highlighted" role="option" data-choice="" data-id="2" data-value="AFG" data-select-text="ກົດເລືອກ" data-choice-selectable="" aria-selected="true">Afghanistan</div><div id="choices--p_nationality-do-item-choice-3" class="choices__item choices__item--choice choices__item--selectable" role="option" data-choice="" data-id="3" data-value="ALA" data-select-text="ກົດເລືອກ" data-choice-selectable="">Åland Islands</div>

According to I have list option as the code and I want to create a javascript to select it refer by data-id
Example I want to make data-value="AFG" selected, how can i do?

Link to comment
Share on other sites

I tried to select option base on data-value

now my javasript is work for 

document.querySelector('.choices__item[data-value="AFG"]').setAttribute('aria-selected', 'true');

But I need to add class name and remove some class name, I tried as below code but it's work

document.querySelector('.choices__item[data-value="AFG"]').addclass("is-selected is-highlighted");

Link to comment
Share on other sites

If you're talking about jshjohnson/Choices then it looks like the setChoiceByValue method is what you need to use.

If you aren't talking about that then a link to its website would be nice - though I do recommend reading said website for information beforehand, in case the answer is there already.

  • Like 1
Link to comment
Share on other sites

Yes, you are right, it is jshjohnson, now i am looking for the way to create javascript to select it.

if on javascript, i have var name = AFG and AFG is a value of select option.

How can I create javascript to select it?

From example is mean create new choice? how about existing choice?

const example = new Choices(element, {
  choices: [
    { value: 'One', label: 'Label One' },
    { value: 'Two', label: 'Label Two', disabled: true },
    { value: 'Three', label: 'Label Three' },
  ],
});

example.setChoiceByValue('Two'); // Choice with value of 'Two' has now been selected.

 

Edited by nitiphone2021
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.