#1
  1. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115

    Identifying active menu item


    Hi guys,

    I am helping out a 'friend' with a site that has some static pages due to the nature of a horrific template. Due to this I am forced to use javascript to identify active menu-items (I prefer doing it serverside but that is no option, pennywise). So i found a little script that looks like it should do the trick but it doesn't

    javascript Code:
     
    jQuery.noConflict();
    // Do something with jQuery
    jQuery(document).ready(function() {
       var pathname = window.location.pathname;
       var pathname = pathname.split('/');
       var tester = pathname[pathname.length-1];
     
       jQuery('#categories li a').each(function(){
           var test = $(this).attr('href');
    // I added this to test the values
           alert('test='+test+'tester='+tester);
           if (test == tester){
               $(this).addClass('active');
           }
     
       });
     
    });


    As you can see in the code It should alert 2 variables, but it doesn't alert anything. Am I missing something?

    Love to hear it

    P.s. the moment I go into the .each function I can't alert stuff. above that function I am able to, which for me is an indication that something is wrong there. (I have no other method of identifying errors with javascript)
    Last edited by aeternus; February 11th, 2013 at 01:01 PM. Reason: spelling
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    you cannot use "$" when you've called jQuery.noConflict();

    Check the JavaScript console of your browser. I'm sure that's exactly the error.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Hi Jacques1,

    Thanks! I kinda missed that dollar sign
    Cheers!!
  6. #4
  7. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    If anyone wants to use this in the future here is the final script that works. (can be optimized probably )

    javascript Code:
     
    jQuery.noConflict();
    // Do something with jQuery
    jQuery(document).ready(function() {
       var pathname = window.location.pathname;
       var pathname = pathname.split('/');
       var tester = pathname;
     
       jQuery('#categories li a,#pages li a').each(function(){
     
           var test = jQuery(this).attr('href').split('/');
           //alert(tester[1]+' | '+test[1]);
           if (test[1] == tester[1]){
     
               jQuery(this).addClass('active');
           }
     
       });
     
    });
    //based on http://tinyurl.com/b8ujzen

IMN logo majestic logo threadwatch logo seochat tools logo