Reputation: 4098
Here is a fiddle for referrence https://jsfiddle.net/mc6jqtzr/
I need to be able to populate the innerHTML of #date
with some content only IF at least one option
in the select list above DOES NOT have a style applied (in this case the display property).
e.g. the last option
does not have a style applied, so the #date
should get populated.
var date = $("#date");
// Show the date only if there are open spots
if ($('.js-check-in-times option:visible').length === 0) {
date.html('');
} else {
date.html('Working now.');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="check-in__select js-check-in-times" id="time" name="AptTime" required="" aria-required="true" aria-label="times filter">
<option value="2021-07-28T08:15:00.000-04:00" data-group="2021-07-28" style="display: none;">08:15 AM</option>
<option value="2021-07-28T08:30:00.000-04:00" data-group="2021-07-28" style="display: none;">08:30 AM</option>
<option value="2021-07-28T08:45:00.000-04:00" data-group="2021-07-28" style="">08:45 AM</option>
</select>
<div id="date" style="font-size: 24px; color: red; font-family: sans-serif;"></div>
Upvotes: 2
Views: 73
Reputation: 4098
A simple, yet working solution:
if ($('.js-check-in-times option[style=""]').length > 0)
This seems to do the trick.
Much thanks to andy mccullough
for pointing out the visibility limitations for select options.
Upvotes: 0
Reputation: 2360
Filter options
based on the value of style
attribute.
If style attribute is absent, compare with null
instead of ''
var date = $("#date");
// Show the date only if there are open spots
var optionsWithEmptyStyle = $('.js-check-in-times option').filter(function() {
return $(this).attr('style') === '';
});
if (optionsWithEmptyStyle.length === 0) {
date.html('');
} else {
date.html('Working now.');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="check-in__select js-check-in-times" id="time" name="AptTime" required="" aria-required="true" aria-label="times filter">
<option value="2021-07-28T08:15:00.000-04:00" data-group="2021-07-28" style="display: none;">08:15 AM</option>
<option value="2021-07-28T08:30:00.000-04:00" data-group="2021-07-28" style="display: none;">08:30 AM</option>
<option value="2021-07-28T08:45:00.000-04:00" data-group="2021-07-28" style="">08:45 AM</option>
</select>
<div id="date" style="font-size: 24px; color: red; font-family: sans-serif;"></div>
Upvotes: 1
Reputation: 467
Check if style is empty
var date = $("#date");
var sel = $("#time");
var opts = sel.find("option");
for(var opt of opts) {
if($(opt).attr("style") !== undefined && $(opt).attr("style") === "") {
date.html("There's Element Haven't Style");
} else {
date.html('Working now.');
}
}
Check if just the visible options and it's style
var date = $("#date");
var sel = $("#time");
var opts = sel.find("option");
for(var opt of opts) {
if($(opt).css("display") !== "none" && $(opt).attr("style") !== undefined && $(opt).attr("style") === "") {
date.html("There's Element Haven't Style");
} else {
date.html('Working now.');
}
}
Upvotes: 1
Reputation: 28522
You can loop through options to get count of option which are hidden then check if the total hidden and total option are same or not depending on this show your message.
Demo Code :
var date = $("#date");
var count = 0;
$('.js-check-in-times option').each(function() {
if ($(this).css("display") == "none") {
count++;
}
})
console.log(count)
// Show the date only if there are open spots
if ($('.js-check-in-times option').length === count) {
date.html('');
} else {
date.html('Working now.');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<select class="check-in__select js-check-in-times" id="time" name="AptTime" required="" aria-required="true" aria-label="times filter">
<option value="2021-07-28T08:15:00.000-04:00" data-group="2021-07-28" style="display: none;">08:15 AM</option>
<option value="2021-07-28T08:30:00.000-04:00" data-group="2021-07-28" style="display: none;">08:30 AM</option>
<option value="2021-07-28T08:45:00.000-04:00" data-group="2021-07-28" style="">08:45 AM</option>
</select>
<div id="date" style="font-size: 24px; color: red; font-family: sans-serif;"></div>
Upvotes: 1