Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Radio Button Checked State Styling
- This topic has 5 replies, 2 voices, and was last updated 5 years ago by
Ernest Marcinko.
-
AuthorPosts
-
May 31, 2021 at 8:56 am #33496
vbrains54
ParticipantHello,
First I would like to say thanks for this great plugin!
We are having a slight issue with it though:
On our development homepage ( near the bottom of http://tlpc.staging.wpengine.com/ ) we have a filter used to sort through specialists.
Everything is working pretty well except for the “checked” state of our Radio Buttons.We are unable to style the clicked radio button to show the user what filter is active at the moment.
Could you please have a look and let me know if you know a way to handle this?
For some reason, it works well on mobile but it might be only because of the hover state.Kind regards,
Viktor B.May 31, 2021 at 10:07 am #33499Ernest Marcinko
KeymasterHi Viktor,
Unfortuantely this is not possible only via custom CSS styling, as CSS does not allow parent node selection based on child node values, you will need custom JS for it.
Luckily this is not too complicated, so I madde a quick custom code for you to handle that. Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.
add_action('wp_footer', 'wp_footer_add_js_button_class'); function wp_footer_add_js_button_class() { ?> <script> (function(){ document.querySelectorAll('.asp_sb_2_1 .asp_label').forEach(function(label){ label.addEventListener('click', function(event) { event.preventDefault(); document.querySelectorAll('.asp_sb_2_1 .asp_label').forEach(function(element){ element.classList.remove('asp_label_selected'); }); this.classList.add('asp_label_selected'); }); }) }()) </script> <?php }This will add the “asp_label_selected” class to the labels. You can use that to make any custom CSS to style the selected button:
.asp_label_selected { // your style here }May 31, 2021 at 10:58 am #33500vbrains54
ParticipantHi Ernest,
Thank you for the quick solution! That was brilliant!
Unfortunately, while this allows us to style the element behavior exactly as we needed, it breaks the filter’s functionality for this taxonomy.
With the code in place when you click on any of the “pills” ( All, Children, Teenagers, etc. ) the filter is not working.Could the event.preventDefault(); be causing this?
I appreciate your time!
Kind regards,
Viktor B.May 31, 2021 at 1:06 pm #33503Ernest Marcinko
KeymasterThat is very likely actually. Can you please try without that line?
June 1, 2021 at 8:36 am #33509vbrains54
ParticipantHi Ernest,
Yes, removing that line fixed it and it is working great!
Thank you very much for your assistance!I have just another quick question:
How would you go about preselecting the first option on those pills to show the user that the filter is active when they first land on the page?I have added `add_action(‘wp_footer’, ‘wp_footer_add_js_button_class_first_selected’);
function wp_footer_add_js_button_class_first_selected() {
?>
<script>
(function(){
document.querySelectorAll(‘label.asp_label’)[0].classList.add(‘asp_label_selected’);}())
</script>
<?php
}`
but for some reason, the first click on the filter is not recognized and only the second click activates the filtering functionality.I would love to be able to fix that and have the first filter option selected by default ( with the new “selected” style applied). But even if it’s not possible it’s all good!
Thank you once again, Ernest!
Kind regards,
Viktor B.June 1, 2021 at 1:01 pm #33511Ernest Marcinko
KeymasterTry this variation instead:
add_action('wp_footer', 'wp_footer_add_js_button_class'); function wp_footer_add_js_button_class() { ?> <script> (function(){ document.querySelectorAll('.asp_sb_2_1 .asp_label').forEach(function(label){ if ( label.querySelector("input[type=radio]").checked ) { label.classList.add('asp_label_selected'); } label.addEventListener('click', function(event) { document.querySelectorAll('.asp_sb_2_1 .asp_label').forEach(function(element){ element.classList.remove('asp_label_selected'); }); this.classList.add('asp_label_selected'); }); }) }()) </script> <?php }It will set the class to the label, where the radio button is preselected by default.
If you are also using elementor live widget filtering, then try enabling the auto populate feature, so the plugin triggers an initial search with the selected arguments.
-
AuthorPosts
- You must be logged in to reply to this topic.