4

I am using a jquery accordion (Jquery 1.9/Jquery-ui-1.10.1). I would like to link to the page from external pages with an anchor tag. When an anchor tag is used I would want to open a specific panel on the accordion. For example, if I pass a #panel2 anchor tag, I would want to open and display the second panel.

I also have images on the accordion page that also need to link to specifc panels in the accordion.

Can anyone help with this?

Kris
  • 41
  • 1
  • 2

1 Answers1

6

Try:

HTML:

<a class="opener" data-panel="0" href="#">Open Section 1</a>
<a class="opener" data-panel="1" href="#">Open Section 2</a>
<a class="opener" data-panel="2" href="#">Open Section 3</a>
<a class="opener" data-panel="3" href="#">Open Section 4</a>

<div id="accordion">

<h3>Section 1</h3>

    <div>
        <p>Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
            ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
            amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo
            ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.</p>
    </div>

<h3>Section 2</h3>

    <div>
        <p>Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
            purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
            velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit
            faucibus urna.</p>
    </div>

<h3>Section 3</h3>

    <div>
        <p>Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
            Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
            ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia
            ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.</p>
        <ul>
            <li>List item one</li>
            <li>List item two</li>
            <li>List item three</li>
        </ul>
    </div>

<h3>Section 4</h3>

    <div>
        <p>Cras dictum. Pellentesque habitant morbi tristique senectus et netus et
            malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus
            orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel
            est.</p>
        <p>Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
            Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
            inceptos himenaeos.</p>
    </div>
</div>

jQuery:

var $accordion = $("#accordion");

$accordion.accordion();

$(".opener").on("click", function () {
    var $this = $(this),
        toOpen = $this.data("panel");

    $accordion.accordion("option", "active", toOpen);

    return false;
});

Fiddle here

darshanags
  • 2,519
  • 1
  • 13
  • 19
  • Thanks for your help, though I still cannot get it to work with external anchor links coming into the page with the accordion. Here is my Fiddle [link]http://jsfiddle.net/kmascianto/FzrSp/ – Kris Mar 12 '13 at 15:08
  • 1
    I don't understand, what do you mean by `with external anchor links coming into the page with the accordion`? – darshanags Mar 12 '13 at 15:22
  • I want to have a page that links to page with the accordion. On the page without the accordion, I want to have several different links with anchors. When you click on a link with an anchor it will go to the page with the accordion and open a specific panel based on the anchor link. For example, there will be 4 links on the page without the accordion. Automotive, Travel, Dining and Entertainment. If I click on the link for entertainment, I want a specific panel to open up on the page that has the accordion. – Kris Mar 12 '13 at 16:33
  • Just to clarify, I would like it work with both internal links on the same page with the accordion (like you previously provided) and with external links with anchor tags. Thanks again! – Kris Mar 12 '13 at 16:57
  • 1
    @Kris try this fiddle: http://jsfiddle.net/VZ3T5/3/ - this will match hashes that looks like #panel1, #panel2, #panel3, etc.. The number has to begin with 0. (Of course you can see this working on jsfiddle - you will have to add this code to you own page). – darshanags Mar 12 '13 at 17:19
  • Sorry to be pain, but I still cannot get it to work. When I use a link with a #panel2 tag nothing happens. – Kris Mar 12 '13 at 17:48
  • I apologize. I still can't seem to get it. Here is a link to a test page I have the code on. Let me know if you can see where I am messing up - http://mckmmarketing.com/test.html. I still can't get the anchor links to work. Thanks again for helping. – Kris Mar 13 '13 at 01:08
  • 2
    Linking to this page (click on the link): http://mckmmarketing.com/test.html#panel3 doesn't open the last panel for you by default? – darshanags Mar 13 '13 at 02:21
  • It works. Sorry, I was trying to access the different anchors from the same browser window. How difficult is it to accept anchor names other than "panel"? Again, thank you so much for all of your quick help! – Kris Mar 13 '13 at 21:50
  • That is a very nice solution! Thanks! – jerrygarciuh Jun 10 '13 at 20:45
  • sorry darshnags... for being a novice... how does this work .... im a little lost ...can you kindly highlight which code goes on the accordion page and what goes on the external page's link. as i understand the link on the external page will just contain the html link of accordion page with #anchorname. thanxs – Moody Dec 26 '13 at 08:44