I'd like to get some help from you guys on a simple filter function that uses jQuery ... this uses, I think (I didn't build it & the developer doesn't work for us anymore), class to filter items and assign a class that has display:none style to hide/show the items.
The issue is that only some of the options (as in option tag) work and some don't. And I can't seem to understand why. All of the codes are below, I really appreciate you guys help in advance.
php (wordpress template)
<div id="openings" class="row cantrevealcompanynamesorry-head" style="padding-top:50px;">
<h2 style="">Current Openings</h2>
</div>
<div class="row careers-filter">
<div class="col-md-6">
<form id="filter-dept" action="">
<label class="col-md-4">Sort by Department:</label>
<select class="col-md-8 filter-dept" name="department">
<option value=""></option>
<option value="sales">Sales</option>
<option value="marketing">Marketing</option>
<option value="product">Product Management</option>
<option value="technology">Technology</option>
<option value="operations">Finance & Administration</option>
<option value="people-numbers">Customer Care</option>
<option value="business-dev">Business Development</option>
</select>
</form>
</div>
<div class="col-md-6">
<form id="filter-loc" action="">
<label class="col-md-4">Sort by Location:</label>
<select class="col-md-8 filter-loc" name="location">
<option value=""></option>
<option value="hq">HQ</option>
<option value="northeast">United States · Northeast (Remote)</option>
<option value="various">Various (Remote)</option>
<option value="central-us">United States · Central US (Remote)</option>
<option value="southeast">United States · Southeast (Remote)</option>
</select>
</form>
</div>
</div>
<div class="careers-listing">
<div class="loading-overlay"></div>
<?php if( have_rows('job_positions') ) :
while ( have_rows('job_positions') ) : the_row();
// display a sub field value
$jtitle = get_sub_field('job_title');
$jurl = get_sub_field('job_url');
$jlfield = get_sub_field_object('job_location');
$jlocation = get_sub_field('job_location');
$jllabel = $jlfield['choices'][ $jlocation ];
$jdfield = get_sub_field_object('job_department');
$jdepartment = get_sub_field('job_department');
$jdlabel = $jdfield['choices'][ $jdepartment ];
$joverview = get_sub_field('job_overview');
?>
<div class="row job-row <?php echo ''.$jlocation.' '.$jdepartment.''; ?>">
<span class="job-dept"><?php echo $jdlabel; ?></span>
<a class="job-title" href="<?php echo $jurl; ?>" target="_blank"><h3><?php echo $jtitle; ?></h3></a>
<?php echo '<span class="job-meta">'.$jllabel.'</span>'; ?></span>
<p><?php echo ''.$joverview.''; ?></p>
<a class="job-button" target="_blank" href="<?php echo $jurl; ?>">Learn More</a>
<div class="clear"></div>
</div>
<?php
endwhile;
else :
echo '<h3 style="text-align:center;">No positions currently</h3>';
endif;
?>
<div class="row search-none"></div>
</div>
</section>
<!--end .section-career-positions-->
Javascript
$( ".careers-filter select" ).change(function() {
var selectValue = $(this).val();
if(selectValue !== ''){
$('.loading-overlay').fadeIn(300, function(){
$('.job-row').removeClass('search-hide');
$('.careers-listing .search-none').empty();
$('.job-row:not(:contains(' + selectValue + '))').addClass('search-hide');
if (!$(".job-row").not(".search-hide").length) {
$('.careers-listing .search-none').text('No positions available');
}
$('.loading-overlay').fadeOut(300);
});
} else{
$('.loading-overlay').fadeIn(300, function(){
$('.job-row').removeClass('search-hide');
$('.loading-overlay').fadeOut(300);
});
}
if($(this).hasClass('filter-dept')){
$('.filter-loc').val('');`enter code here`
} else if($(this).hasClass('filter-loc')){
$('.filter-dept').val('');
}
});
ACF Field Names *** Only "Sales" tab works properly, but class names are being passed correctly. This is why I think javascript is the issue, and job_location tab works correctly.
job_department
sales : Sales
marketing : Marketing
product : Product Management
technology : Technology
operations : Finance & Administration
people-numbers : Customer Care
business-dev : Business Development
job_location
hq : HQ
northeast : United States · Northeast (Remote)
various : Various (Remote)
central-us : United States · Central US (Remote)
southeast : United States · Southeast (Remote)
Finally, I figured it out (yay!!). Thanks to http://api.jquery.com/not-selector/ , when I looked that page up, it said,
The .not()
method will end up providing you with more readable selections than pushing complex selectors or variables into a :not() selector filter. In most cases, it is a better choice.
So, I changed this,
$('.job-row:not(:contains(' + selectValue + '))').addClass('search-hide');
to this,
$( ".job-row" ).not( document.getElementsByClassName( selectValue ) ).addClass('search-hide');
And it's working properly. I'm leaving my answer here for people who have similar issues with ":not() selector"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.