views:

90

answers:

1

I started to implement some new html5 features (standards, nothing fancy) in my project. Just the standard header, footer, aside, ect. For some reason a javascript code that I used on a past project doesn't work now and I can't figure out what the problem is.

I compared the code (html/javascript) with my new project and the past project (with javascript working) and I don't see any difference. The only thing I can think of is the change in html versions.

By the way Im trying to implement a script that highlights a current link from a menu. It is supposed to use javascript to add/remove a ".selected" code the anchor tags in the menu and relates to the current page and link.

here is the code:

<aside>
        <section>
            <Strong>Quick Links</strong>
                <menu id="side_menu">
                    <ul>
                        <li><a href="application.php">Sign Up</a></li>
                        <li><a href="testimonials.php">Testimonials</a></li>
                        <li><a href="diploma.php">The Process</a></li>
                        <li><a href="diploma.php">Course Listings</a></li>
                        <li><a href="about.php">American High School</a></li>
                     </ul>
                </menu> 

        <script>
            $(document).ready(function() {
                var loc = window.location.href; // The URL of the page we're looking at
                $('#side_menu a').each(function() {
                    if (loc.indexOf(this.href) !== -1) { // If the URL contains the href of the anchor
                            $(this).addClass('selected'); // Mark it as selected
                    }
                });
            });
        </script>

        </section>
</aside> 

Here is the link to the site here (Side Panel)

I would appreciate any help on this issue. I spent hours trying to figure this out. Thanks for any help.

gdinari

+2  A: 
T.J. Crowder
Thanks for the quick response! The funny thing is that I did what you said and it ended up fixing another js problem I had, but the side menu highlights still don't work. Do you think it could be the reference that Im using in the js? I first used "#side_menu a" then I tried "aside li a," but it didn't work... Do you know what the problem could be?
gdinari
@gdinari: Looks to me like you're trying to use jQuery code on a page/site that is set up to use Prototype, and not including jQuery. Updated my answer.
T.J. Crowder
Ok what I tried was converting the prototype.js with the code that you provided, but noting. I guess I have to use the JQuery.noConflict with my original code? Im not completely sure, javascript/jquery is new to me. I'll thoroughly go through your advice and try to make it work. If you have any other suggestions, please let me know. I appreciate the help.
gdinari
@gdinari: There was a bug in my translation from jQuery -> Prototype (I should have used `addClassName`, not `addClass`). The translation is functional now. I don't know whether it's necessarily what you want, but it seems to basically work: http://jsbin.com/uriyi3/4 In that example, there are four links, two of which are to that same page (one absolute, one relative). If they match the page location, they should get a grey background. For me, they do. Good luck with it.
T.J. Crowder
Thanks so much! I looked over some of the past answers to my other questions but couldn't apply the logic to this situation. But the sample in link you provided helped a lot. I have to do a lot reading up on js. Thanks again.
gdinari
@gdinari: No worries, glad that helped.
T.J. Crowder