I am using the Chosen library. I have a long hierarchy select drop down list of parent and child items. It is automatically populated. I want to hide all li’s containing text that starts with a dash character, example -General Discussion. I would also like to scope the hiding to only inside of the ul class name chosen-results.
<ul class="chosen-results">
//Below is the li of a child (I want to hide this li)
<li class="active-result" data-option-array-index="4">-General
Discussion</li>
//Below is the li of a parent ( I want to leave this alone)
<li class="active-result" data-option-array-index="0">Accident Prevention
</li>
</ul>
Updated. This works on node preview mode, but not in node edit mode
(function ($) {
$(document).ready(function () {
$("ul.chosen-results li").each(function () {
if ($(this).text().charAt(0) === '-') {
$(this).hide();
}
});
});
})(jQuery);
Below I tried to use .ajaxComplete, but it does not work.
(function ($) {
$(document).ajaxComplete(function() {
$("ul.chosen-results li").each(function () {
if ($(this).text().charAt(0) === '-') {
$(this).hide();
}
});
});
})(jQuery);
2
Answers
You can use JavaScript/jQuery to hide the li elements with the text starting with a dash character. Here is a sample code:
This code will run when the document is ready and select all li elements inside the ul with the class chosen-results. It will then loop through each li element and check if the first character of its text is a dash. If it is, the li element will be hidden.
You don’t need jQuery.
textContent
to remove wrapping whitespaces^-
(starts with dash)String[i]
to match a single character at String indexdisplay: none;
)Using RegExp.prototype.test()
Using String.prototype.startsWith()
Using String index [i]