Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery - Remove Select Items (loop through array)
    primarykey
    data
    text
    <p>I have the following code I am using to remove select items based on the var "ColTypeCus".</p> <pre><code>$(document).ready(function() { var ColTypeCus = '$FooVar'; if (ColTypeCus.substr(0, 21) == "Machinery") { $("select#ctl00_PageBody_lstValuationSource option[value='3']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='1']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='2']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='4']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='5']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='12']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='13']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='14']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='15']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='16']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='18']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='19']").remove(); } else if (ColTypeCus.substr(0, 7) == "Banking") { $("select#ctl00_PageBody_lstValuationSource option[value='17']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='3']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='7']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='8']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='9']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='10']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='11']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='12']").remove(); } else if (ColTypeCus.substr(0, 19) == "Inventory") { $("select#ctl00_PageBody_lstValuationSource option[value='3']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='1']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='2']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='4']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='5']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='12']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='14']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='15']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='16']").remove(); $("select#ctl00_PageBody_lstValuationSource option[value='18']").remove(); } }); </code></pre> <p>It works great, but I wanted to know if there was a more elegant way (fewer lines of code) I could implement to do the same thing.</p> <p>One idea was to specify which items to remove listed in array and loop through the items.</p> <pre><code>***pseudocode*** var ColType = '' var RemoveThis = '' if Coltype = "Machinery" RemoveThis = ('1,3,5,7,9'); { $("select#ctl00_PageBody_lstValuationSource option[value='+RemoveThis+']").remove(); } elseif Coltype = "Banking" RemoveThis = ('2,4,6,8'); { $("select#ctl00_PageBody_lstValuationSource option[value='+RemoveThis+']").remove(); } </code></pre> <p>Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload