Search code examples
jqueryselectdropdown

How do I disable or hide an option in a dropdown list which has no class or ID


<select name="wpeevent_button_qty_a"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option></select>

This is the code. I guess I want to use javascript / jquery to either add a CSS style of hidden to element 1, or to disable or delete the element. Just not sure how to target it since it has no class or id?!

Trying this and its not working,

$('main-table_5764 select option').attr("disabled","disabled");

here's the fiddle

https://jsfiddle.net/86dm34kL/1/


Solution

  • Use selector

    https://learn.jquery.com/using-jquery-core/selecting-elements/

    $('select[name="wpeevent_button_qty_a"] option[value="5"]').attr("disabled",true);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <select name="wpeevent_button_qty_a"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option></select>