Avatar of Aaron Roessler
Aaron Roessler
 asked on

Code is working but keep getting console error, should I ignore?

My code is working but i get a console error: Should I ignore it?
Uncaught Error: Syntax error, unrecognized expression: #lips/

URL is https://allgood.flywheelsites.com/shop/#lips/

Here is my code: the trailing slash was breaking this code but i finally got it working.
var url = window.location.href.replace(/\/$/, '');
var lastPart = url.substr(url.lastIndexOf('/') + 1);
if (lastPart === "#lips") {
jQuery('input.jet-checkboxes-list__input').each(function(){
var catagory_value=jQuery(this).val();      
for (var i=0;i<catagory_value[0].length;i++) {
if (catagory_value == "129") {
jQuery(this).parent().addClass("lips");
jQuery(window).load(function() {
      jQuery('.lips .jet-checkboxes-list__input').trigger('click');
});
} } }); }
jQueryWordPress

Avatar of undefined
Last Comment
Aaron Roessler

8/22/2022 - Mon
Bill Prew

If you are going to an anchor on that page, which the URL syntax seems to indicate, then why do you have the trailing slash.  Typically that would be:

https://allgood.flywheelsites.com/shop/#lips

Which doesn't cause the error.


»bp
Aaron Roessler

ASKER
I am not using an anchor for anchor purposes, using it for my Jquery to check if url ends with "#lips or #deodorants or etc..".  Then click a certain checkbox.

I installed a Wordpress plugin called "Redirection" and am redirecting all Woocommerce product-category urls..
Here is a sample:
https://allgood.flywheelsites.com/product-category/lips  Redirects to  https://allgood.flywheelsites.com/shop/#lips

We use a general SHOP page that will Filter results using checkboxes. It seems wordpress keeps adding the ending slash which breaks my jquery code.

So I managed to get everything working without errors by changing my code that enqueues the script.

add_action('wp_enqueue_scripts', 'ag_enqueue_scripts');
function ag_enqueue_scripts() {
    wp_enqueue_script( 'ag_scripts', get_stylesheet_directory_uri() . '/js/ag_scripts.js');
}
Arana (G.P.)

have a look at this, it may help you make your code work better with or without the slashes

https://tommcfarlin.com/trailing-slash-in-wordpress/

https://developer.wordpress.org/reference/functions/untrailingslashit/
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Aaron Roessler

ASKER
Here is my working script. I have 10 hash urls i need the code to check for so I duplicated this 10 times each with different #. here they are (lips, deodorants, gift-sets, kids, skin, all-good-goop, coconut-oils, herbal-freeze, lotions, sun )
	var url = window.location.href.replace(/\/$/, '');
	var lastPart = url.substr(url.lastIndexOf('/') + 1);
	if (lastPart === "#deodorants") {
jQuery('input.jet-checkboxes-list__input').each(function(){
var catagory_value=jQuery(this).val();	
for (var i=0;i<catagory_value[0].length;i++) {
if (catagory_value == "130") {
jQuery(this).parent().addClass("deodorants");
	jQuery(window).load(function() {
		jQuery('.deodorants .jet-checkboxes-list__input').trigger('click');	
	});
	
} } }); }

Open in new window