I am running a WordPress site (Divi themes), and it has the ability to add code to specific pages. I have a page with a left sidebar showing the table of contents, a middle text with a description, and a right sidebar with an example. The site is here: logan.pverify.com/rest-api.
When the user clicks on the table of contents, it’s a hyperlink bookmark #foo. That part is fine. But I want javascript to launch and display code examples on the right sidebar (i envision this to be a series of divs that are hidden or visible). There are only 5 or so sections to the whole thing so it’s not all that bad.
Anyway. I can’t figure out if it is even possible to do this. I managed to get js code working. Laughable, but it’s a start. I am mostly a C# winforms guy.
<script>
jQuery(function($){
alert('hello');
});
</script>
4
Answers
I ended up doing a few things, which I thought I would mention. Again this is for a wordpress site. The net effect is that the "main" TOC headers will link to that section of the text as a bookmark. The code will show all the divs. The subheaders with individual endpoint documentation will hide all the divs except for one main and one example. So the net effect is that at the top of the page you can see both an example and description. Thanks to everyone, this is probably a mix of what everyone was thinking.
On the left there is a sidebar for the table of contents, and it's code is here:
The main body has code like this:
The right sidebar with the examples has code like this:
And the JS is in a code module in Divi.
All you need to do is first make the element hidden by default in the CSS, and then attach a click event handler to the visible element with the
.on() method
. This handler should change the CSSdisplay
property of the invisible element (in this example, toblock
) with use of the.css() method
:Hope this helps! 🙂
You will need to give a unique
id
and aclass=bookmark
to each of the hiddendiv
that you envision that corresponds with a hyperlink bookmark on the table of content. For instance, the link #foo should have a hidden div with idfoo
. Add anonclick=showSection(this)
handler to each link in the table of content and a functionshowSection
that looks likeI assume the bookmark links are html
anchor
tags in the formfoo.com/#bar
HTML
jquery
$(".nav a").click(function(){
Css
Working Example
http://jsfiddle.net/8u7n2/
This will just hight light the div
http://jsfiddle.net/3gkAV/
They are two different examples and will give you a good idea.