/    Sign up×
Community /Pin to ProfileBookmark

Open an accordion in same window by anchor tag

I am trying to open an accordion from an anchor tag in the same window. But I have to click twice to get it to work. Any clue how to open an accordion (details) with only ONE click?

https://jsfiddle.net/4jzfhvuo/3/

to post a comment
HTMLJavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumNov 02.2021 — What's happening is as follows:

After loading the page there is no hash present in the URL. Thus at first click the element to be opened cannot be determined and it doesn't open. However one default action of clicking a link that contains a hash is, that it's added to the URL.

Therefore at second click the hash is there and subsequently the element can be determined and opened.
Copy linkTweet thisAlerts:
@sibertauthorNov 02.2021 — > @Sempervivum#1638964 Therefore at second click the hash is there and subsequently the element can be determined and opened.

Yes, I figured out that that this happen. But how do I open an accordion in the same page? Any tip?
Copy linkTweet thisAlerts:
@SempervivumNov 02.2021 — Read the hash from the link having been clicked instead of the URL:
``<i>
</i> function openacc(ele) {
// var hash = location.hash.substring(1);
const dest = ele.getAttribute('href').substring(1);
document.getElementById(dest).open = true;
}<i>
</i>
`</CODE>
When doing so you have to hand over the element to the function:
<CODE>
`<i>
</i> &lt;div&gt;
&lt;a href="#1" onclick="openacc(this)"&gt;#foo&lt;/a&gt;
&lt;a href="#2" onclick="openacc(this)"&gt;#bar&lt;/a&gt;
&lt;/div&gt;<i>

</i>
``
Copy linkTweet thisAlerts:
@sibertauthorNov 02.2021 — Yes. This works in jsfiddle, but in real website it does not open the accordion. I had to "open" the window again:

``<i>
</i>function openacc(ele) {
const dest = ele.getAttribute('href').substring(1);
document.getElementById(dest).open = true;
window.open(ele);
}<i>
</i>
``

But this is causing a "flicker" that is annoying.

Is there anyway to "refresh" without flickering?
Copy linkTweet thisAlerts:
@SempervivumNov 02.2021 — Hm, in my testfile on Opera and XAMPP it's working fine.

I propose to do some debugging:
``<i>
</i>function openacc(ele) {
console.log(ele.getAttribute('href'));
const dest = ele.getAttribute('href').substring(1);
document.getElementById(dest).open = true;
// window.open(ele);
}<i>
</i>
`</CODE>
My first attempt was this:<br/>
<C>
const dest = ele.href.substring(1);`

and it was not working as the browser extended the href attribute to a full URL including protocol and domain.
Copy linkTweet thisAlerts:
@SempervivumNov 02.2021 — ... or use data attributes:
``<i>
</i> &lt;details id="1"&gt;Details
&lt;summary&gt;Foo&lt;/summary&gt;
&lt;/details&gt;
&lt;details id="2"&gt;Details
&lt;summary&gt;Bar&lt;/summary&gt;
&lt;/details&gt;

&lt;br&gt;
&lt;div&gt;
&lt;a data-dest="1" onclick="openacc(this)"&gt;#foo&lt;/a&gt;
&lt;a data-dest="2" onclick="openacc(this)"&gt;#bar&lt;/a&gt;
&lt;/div&gt;
&lt;script&gt;
function openacc(ele) {
const dest = ele.dataset.dest;
document.getElementById(dest).open = true;
}
&lt;/script&gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@sibertauthorNov 02.2021 — I finally found the issue why it is not working in real life. I have placed an anchor INSIDE one of the &lt;details&gt;. An anchor outside the details, work as expected. Strange is that the "open" status is set on the accordion, but disappear immediately.

Is there any way to get it to work both inside and outside the accordion?

https://jsfiddle.net/bv90d83g/3/
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...