Newman schrieb:
> Hello there,
>
> I've been been building an FAQ page that displays a list of questions but
> keeps the answers hidden until the question is clicked.  I've added a bit of
> jquery to animate the show/hide stuff and that all works fine.  However, the
> FAQ questions need to be linked to from other areas of the site as well. 
>
> I've added IDs to all the questions and I'm linking to them with anchor
> links (e.g. faqs.html#q-1).  When the page loads the script needs to find
> the corresponding ID and display the answer div.
>
> I've written a function to strip the anchor link from the url and and
> display the answer when the page loads. 
> function anchorLinkShow() {
>       
>               anchorLink = $(location).attr("href");                //finding 
> url in
> address bar
>
>               if(anchorLink.indexOf("#")!=-1){
>                       anchorLink = anchorLink.split("#")[1];       
> //stripping url and #
>               }
>
>               if (document.getElementById(anchorLink)) {      //find heading 
> with
> matching id
>
>                targetElement = document.getElementById(anchorLink); 
>
>                $(targetElement).next().slideDown("slow");        // showing 
> div that
> follows heading
>               
>       }
> } 
>
> The issue is that I can't work out how to do this without using
> getElementByID.  I'm fairly new to javascript and Jquery and I was wondering
> if anybody could give me any tips as to how to make this a bit more elegant. 
> Any help would be much appreciated.
>
> Cheers,
> Tom
>  
>   
The getElementById stuff might be replaced with this:

// the anchors name is also stored in location.hash
var anchorLink = location.hash;
// as the '#' is AFAIK stored within location.hash
// this might work...
$(anchorLink).next().slideDown("slow");

otherwise, try this.

// targetElement stores the name  as you processed it.

$('#' + anchorLink).next().slideDown("slow");


You might also iterate over the resulting elements - or specify one. 
each() or eq() would perhaps help.

-- Marc

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to