0
<script>    
$("ul#sol_menu li a").addClass(function()) {
var current_page = document.location.href;
if ($(this).attr.("href").match(current_page)) {
$(this).addClass('highlight');
};
});
</script>

What is wrong with this?

4
  • 4
    Every single line (but one) has multiple things wrong with it. You need to learn Javascript syntax and jQuery. Commented May 15, 2011 at 23:05
  • You know that browsers have an error console, right? :) Commented May 15, 2011 at 23:12
  • @SLaks, which one, the </script> line? O.o Commented May 15, 2011 at 23:13
  • @David: the var currentPage Commented May 15, 2011 at 23:22

2 Answers 2

5

I believe this might be what you were going for...
And as SLaks pointed out, your syntax (in this case) is a bit atrocious...

<script>    
    $(document).ready(function(){
        var current_page = document.location.href;
        $("ul#sol_menu li a").each(function(){
            if ($(this).attr('href') == current_page) {
                $(this).addClass('highlight');
            }
        });
    });
</script>

So to answer your question... the following was wrong with your code:

  1. Depending whether or not your script tag was before or after your ul element, you need to use the $(document).ready function
  2. You weren't using the addClass callback properly...
  3. You had an extra bracket at the addClass callback "addClass(function()) {" should have been "addClass(function() {"
  4. There's no .match method like the one you've used...
  5. You had a syntax error here: '.attr.("href")'; no period after '.attr'

    Hope that answers your question.
Sign up to request clarification or add additional context in comments.

Comments

4

I think this is what you're trying to do, with comments so hopefully you learn something about Javascript/jQuery:

// when DOM is ready
$(function(){    

    // cache current URL
    var current_page = document.location.href;

    // use .each method to check link hrefs against current location
    $("ul#sol_menu li a").each(function () {

        // if this link is for the current page, highlight it
        if (current_page.indexOf(this.href) >= 0) {
            $(this).addClass('highlight');
        };

    });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.