I’m trying to hide subcategories in a search drop-down menu in wordpress by specific text using javascript. so far without luck. I’m no programer so i’ll appreciate your help with directions or fix to the script.
The menu html looks somewhat like this when i inspect the page(i’ve narrowed it down):
<ul>
<li>
<select class="default-select select2-hidden-accessible" name="categories" tabindex="-1" aria-hidden="true">
<option value=""> choose category </option>
<option value="119">Customer Support</option>
<option value="163">Hosting</option>
<option value="181"> - Link Building</option>
</li>
</ul>
When I inspect the options i get this:
<span class="select2-results">
<ul class="select2-results__options" role="tree" id="select2-categories-85-results" aria-expanded="true" aria-hidden="false">
<li class="select2-results__option" role="treeitem" aria-selected="false"> choose category </li>
<li class="select2-results__option" id="select2-categories-85-result-5nsl-119" role="treeitem" aria-selected="false">Customer Support</li>
<li class="select2-results__option" id="select2-categories-85-result-td3l-163" role="treeitem" aria-selected="false">Hosting</li>
<li class="select2-results__option" id="select2-categories-85-result-c6bv-181" role="treeitem" aria-selected="false"> - Link Building</li>
</ul></span>
I’m trying to hide every li that contain the character "-" or "–".
so after a lot of searching i found this javascript:
let lis = document.getElementsByTagName('li');
for (let x = 0; x < lis.length; x++) {
let li = lis[x];
let content = li.textContent;
if (content.indexOf('-') !== -1 || content.indexOf('--') !== -1) {
li.style.display = 'none';
}
}
The problem with this is when i use it the category menu disappears completely, and the category menu html code looks like this:
<ul>
<li style="display: none;">
<select class="default-select select2-hidden-accessible" name="categories" tabindex="-1" aria-hidden="true">
<option value=""> choose category </option>
<option value="119">Customer Support</option>
<option value="163">Hosting</option>
<option value="181"> - Link Building</option>
</li>
</ul>
I’ve tried using "getElementsByClassName(‘select2-results__option’)" but it does absolutely nothing, like the menu and its content is not affected.
how can i fix this? Appreciate your help.
EDIT:
HTML PHP snippet of search section if it helps:
<ul>
<li>
<div class="form-group">
<input type="text" placeholder="<?php echo __( 'What are you looking for?', 'exertio_theme' ); ?>" class="form-control" name="title" value="<?php echo esc_attr($title_value); ?>">
</div>
</li>
<li>
<select class="default-select" name="categories">
<option value=""> <?php echo __( 'Select Category', 'exertio_theme' ); ?> </option>
<?php echo get_hierarchical_terms('service-categories','', $cat_value ); ?>
</select>
</li>
<li> <button type="submit" class="btn btn-style btn-block"><?php echo __( 'Search', 'exertio_theme' ); ?></button> </li>
</ul>
in addition, when i use the following css to hide specific li in the drop-down menu it works (if that helps):
.select2-results__options > li:nth-child(2) { display: none; }
2
Answers
managed to solve the problem, Thanks to @ikhvjs for providing scripts and helping i learned alot. i'll share solution if anyone is interested.
wordpress javascript file name: select2.full.min.js
The javascript that generated category items dynamically to the page is the following:
i simply added the following variable and if condition after the first variable declaration in the function:
now when i'm in the "/services-search/" page the sub categories that include "-" or "--" do not display.
Your HTML has some syntax errors. You miss a
</select>
and I fixed this.As you have different html source and I am not sure which one you are using.
I just take the first html source.
I updated your js files and the example below: