in order to get the same behavior as the selection element that you might want to extend, for example, like a regular class. maybe use a library like Polymer, github.com/richardanaya/webblock or github.com/SaulDoesCode/Crafter.js to create your own element this way.
Otherwise, you should manually use getters and setters.
You will need to use Object.defineProperty to create this custom behavior using getters and seters on elements.
here is a small little function that will simplify
function newGetSet(element ,key, get, set) { Object.defineProperty(element, key, { set: set, get: get }); } // then use it - just a rough example newGetSet( myElement , 'selectedIndex' , index => { // perhaps then apply a class or something to make it active document.querySelectorAll('ul > li')[index].classList.add('active'); }, () => { let active_index = 0; Array.from(document.querySelectorAll('ul > li')).forEach((item,index) => { if(item.classList.contains('active')) active_index = index; }); return active_index; });
Of course, as mentioned in another answer, in order to get the desired effect from the elements in the user selection list, it is possible to attach an EventListener.
so assume that the "selectedIndex" property is defined in the parent element, so you can just add Listeners, which will update the parent element.
[...document.querySelectorAll('ul > li')].forEach((item,index) => { item.addEventListener('click',evt => { item.parentNode.selectedIndex = index; }); });
source share