Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

html - Move Selected Item to Top of Listbox using javascript using Javascript

I have bind listbox using apex. Below is my code

 <select id="{!$Component.multiselectPanel}:rightList" 
          class="multilist" multiple="multiple" size="{!size}" 
          style="width: {!width};">
      <apex:repeat value="{!rightOptions}" var="option">
          <option value="{!option.value}">{!option.label}</option>
      </apex:repeat>
  </select>

When I click on Top Button that time Selected Item move on top. For Example, In below Image when I clicked on top button that time 'Location' text become first then 'Demo Site' and other values are shown in listbox.

Reorder

Here, I also put my JavaScript code for reference :

function(idList, idHidden) {            
        listBox = document.getElementById(idList);

        var len = listBox.options.length;

        if (len > 0 && listBox.options[0].selected == true) {
          return;
        }
        else {
            listBox.insertBefore(listBox.options[0],
                listBox.options[listBox.selectedIndex]);
          }
}
question from:https://stackoverflow.com/questions/65886446/move-selected-item-to-top-of-listbox-using-javascript-using-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this, hope it helps you

listBox.insertBefore(listBox.options[listBox.selectedIndex], listBox.childNodes[0]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...