I am looking at how to get an array of option elements from a select dropdown with the certain values and then I am going to hide them.
My HTML so far is:
<select>
<option value="1">1 AM</option>
<option value="2">2 AM</option>
<option value="3">3 AM</option>
<option value="4">4 AM</option>
<option value="5">5 AM</option>
<option value="6">6 AM</option>
<option value="7">7 AM</option>
<option value="8">8 AM</option>
<option value="9">9 AM</option>
</select>
What I want to do is get certain options with the values of certain numbers and hide those.
Should be simple enough.
The long winded way I was going to do it. Would be something like:
$('option[value="1"],option[value="2"],option[value="3"],option[value="6"]').remove();
But that is not sustainable once I get a lot more values generated into the option list.
How would I create the array of elements I need with the values I need and then use the .remove() to remove the elements.
Thanks
UPDATE/EDIT - PART TWO OF QUESTION: (answered my own solution below)
Got this working using based on another solution [https://stackoverflow.com/questions/63804234/jquery-filter-select-options-by-array], works for the most part.... but see later below.
var myArray = ["0800", "0805", "0810", "0815", "0820", "0825", "0830", "0835", "0840" ];
// this goes by a step of 5 from 0800 to 1000
$("#sessionStartTime option").filter(function ()
{
return $.inArray($(this).val(), testArray) > -1;
}).remove();
That said, second part of a question than.
Would there be a way to get a range of times or a range of number and toss those into an array as the numbers. You will see the values I have are coming back of what I think are "octal". But in the return array it returns the numbers without the leading "0".
I have looked up a few ways of doing this but nothing seems to do what I need to. I thought about using a '.contains()' or something of the sort but not sure how to implement it. I don't have control over the values building the list because those come from a JSON call that populates the select option list.
This is what I have so far for my range but the leading zero gets omitted but the leading zero is NEEDED to filter the options. Based of this example [https://jasonwatmore.com/post/2021/10/02/vanilla-js-create-an-array-with-a-range-of-numbers-in-a-javascript]
const end = 1000;
const start = 0800;
const step = 5;
const arrayLength = Math.floor(((end - start) / step)) + 1;
var timeArray = [...Array(arrayLength).keys()].map(x => (x * step) + start);
console.log("timeArray:" + timeArray);
//Returns numbers without leading zeros but I need the leading zeros