samedi 14 février 2015

How do I check if the window has scrolled to a href and then execute a function in javascript/jQuery?


I am using this accordian and I have a link on an external page, (see below).


I would simply like when the user clicks on that external link, a specific accordion section would open on the new page (page2.html in this case). (I know you can have a area open of your choosing in the default section of the .js but that's different).


I tried this.


The link on the external page.



<a href="page2.html#Education">foo</a>


Set up an ID on the page with the accordion.



<a id="Education" href="#Education">Education<span class="st-arrow">Open or Close</span></a>


And some jQuery on the accordion page to handle that selection.



$(function() {
$('#st-accordion').accordion();
if ($('a[href="#Education"]') == $(window.location.hash)) {
$('#st-accordion').accordion();
// I also have tried calling the `accordion();` on the `$('a[href="#Education"]')` to activate it.
}
});


I am close, sort of. All I get is when the user clicks from the external page, the user get pushed down to the correct link, but I can't get it to uncollapse).


Thanks in advance!


UPDATE


I tried this.



$(document).ready(function() {
$('#st-accordion').accordion();

$('#Education').focus(function() {
console.log('test'); // <-- This worked in the console.

$('a[href="#Education').accordion({open:2}); // <-- This did not.

});
});




Aucun commentaire:

Enregistrer un commentaire