Skip Navigation

[Resolved] I need help with Javascript to open an accordion from url

This thread is resolved. Here is a description of the problem and solution.

Problem:
I need help with Javascript to open an accordion from url

Solution:
There was a JS error due to how user added the custom JS.

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/i-need-help-with-javascript-to-open-an-accordion-from-url/#post-1801391

Relevant Documentation:

This support ticket is created 4 years, 3 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by johannaH 4 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#1800633

Tell us what you are trying to do?
The URL contains the ID of a post being displayed in and accordion. I am trying to get the relevant card to open automatically when the page is accessed with the anchor. I had this working on another toolset site, but this is no longer working, and js is not exactly my forte
the script looks like this:
jQuery(document).ready(function() {
var url = document.location.toString();
if ( url.match('#') ) {
var hash = url.split('#')[1];

$('#' = hash ).removeClass('collapsed');
// expand the requested panel
$('collapse' + '#' + hash).addClass('show');
}
});

When I try to run it. it get the following in the console
Uncaught TypeError: $ is not a function
at HTMLDocument.<anonymous> ((index):3662)
at i (jquery.js?ver=1.12.4-wp:2)
at Object.fireWith [as resolveWith] (jquery.js?ver=1.12.4-wp:2)
at Function.ready (jquery.js?ver=1.12.4-wp:2)
at HTMLDocument.J (jquery.js?ver=1.12.4-wp:2)

Without all this, the accordion works perfectly fine
btw, where would be the correct location in the view to add the js? Does it matter?

Is there any documentation that you are following?
all over the internet. I've tried various things
Is there a similar example that we can see?

What is the link to your site?
hidden link

#1801391

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I see you added some JS code but JS code you are using is not correct.

When I visit the problem URL you shard I can see the following JS error with the browser's console:

Uncaught TypeError: $ is not a function

The following line of code:

$('#' = hash ).removeClass('collapsed');

require to replace as:

$('#'+hash ).removeClass('collapsed');

Can you please try to use the following code:

jQuery(document).ready(function($) {

var url = document.location.toString();

if ( url.match('#') ) {

var hash = url.split('#')[1];

$('#'+hash ).removeClass('collapsed');

// expand the requested panel
$('collapse' + '#' + hash).addClass('show');

}

});

Do you see it working?

#1801455

My issue is resolved now. Thank you!