quorious quorious - 7 months ago 56
jQuery Question

Disable elements in dropdown after or before an index

Hi i have two requirements to disable all the elements in drop down after and before selected index.

$(document).ready(function() {
$('#test option:not(:selected)').attr('disabled', true);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="tests" id="test">
<option value="1846">test</option>
<option value="1962">test2</option>
<option value="1846">test3</option>
<option value="1962">test4</option>
<option value="1846" selected>test5</option>
<option value="1962">test6</option>
<option value="1846">test7</option>
<option value="1962">test8</option>
<option value="1846">test9</option>
<option value="1962">test10</option>
<option value="1846">test11</option>
<option value="1962">test12</option>

As of I'm able to disable all elements which are not selected but not sure how to put a filter based on selected index.


You can do something like this :

$('#test option').each(function(index,value) {
    if(index > 2) {
        $(this).attr('disabled', true);