[Resolved] Adding custom JS that works on page load AND after updating Views filters
This thread is resolved. Here is a description of the problem and solution.
Problem:
Client is adding custom JS to modify the behaviour of Views filter controls, but the JS only works on initial page load and not when the page has been updated via ajax after changing existing filters.
Solution:
Views provides a series of custom JS events available when using ajax, e.g. for updating content when filters are changed or on pagination, which can be inserted using the Front-end Events button in the custom JS box.
The correct format for adding custom code to the dom ready event and a custom Views event would look like this:
( function( $ ) {
$( document ).ready( function(){
// Code you want to run when the page loads
});
$( document ).on( 'js_event_wpv_parametric_search_form_updated', function( event, data ) {
// Code you want to run when filters have been changed
});
})( jQuery );
This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
it only works when "Always show all values for inputs" is on, and not when "Show only available options for each input" is on.
any ideas?
thanks!
PS: i really think it would be a good suggestion to either add this functionality to views (add a class to the parent label of a checked radio, that is), or instead just design the html differently, so that the parent .radio gets a class.
When you use the "Show only available options for each input" option the filters are updated via ajax whenever one is changed and your JS added with dom ready is lost.
You need to re-attach it, for which you can use a custom Views JS event.
Note in the custom JS box there is a Front-end Events button, which brings up a dialog as per my screenshot, which highlights the event you need to use.
perfect! missed that button.
now the first code works, but the second one, in charge of making the first radio checked at the beginning - doesn't.
i've tried placing it outside the event, but still doesn't work.
where do i put this if i want it to load on page load?
That custom event fires when the filters are changed and the results updated.
If you want something to run on page load you will also have to use the dom ready event, i.e. you may need to duplicate your code, running initially with dom ready, and again with the custom event.
You can't put something which is triggered on dom ready inside a function which is triggered by the custom event because the dom ready event happens first.
I haven't looked too closely at your actual code, just at how you are structuring it.
You need to do something like this:
( function( $ ) {
$( document ).ready( function(){
// Code you want to run when the page loads, e.g.
$('.radio label input[type="radio"]').click(function () {
$('input:not(:checked)').parent().removeClass("checkedpoem");
$('input:checked').parent().addClass("checkedpoem");
});
$('input:checked').parent().addClass("checkedpoem");
});
$( document ).on( 'js_event_wpv_parametric_search_form_updated', function( event, data ) {
// Code you want to run when filters have been changed
$('.radio label input[type="radio"]').click(function () {
$('input:not(:checked)').parent().removeClass("checkedpoem");
$('input:checked').parent().addClass("checkedpoem");
});
$('input:checked').parent().addClass("checkedpoem");
});
})( jQuery );
( function( $ ) {
$( document ).ready( function(){
// Code you want to run when the page loads, e.g.
$(".radio:first-child label input").attr("checked", true).trigger('change');
});
$( document ).on( 'js_event_wpv_parametric_search_form_updated', function( event, data ) {
// Code you want to run when filters have been changed
$('.radio label input[type="radio"]').click(function () {
$('input:not(:checked)').parent().removeClass("checkedpoem");
$('input:checked').parent().addClass("checkedpoem");
});
$('input:checked').parent().addClass("checkedpoem");
});
})( jQuery );