tags:

views:

63

answers:

2

Hello, i need some help with a menu. See the following menu:

menu

The code for this menu:

    <div id="menu">
  <ul>
   <li class="home"><a href="#home" class="panel">home / <span class="go">you are here</span></a></li>
   <li class="about"><a href="#about" class="panel">about / <span class="go">go here</span></a></li>
   <li class="cases"><a href="#cases" class="panel">cases / <span class="go">go there</span></a></li>
   <li class="photos"><a href="#photos" class="panel">photos / <span class="go">maybe here</span></a></li>
   <li class="contact"><a href="#contact" class="panel">contact / <span class="go">or even here<span></span></a></li>
  </ul>
 </div>

What i want to do: onclick a menu item: 1. change the red text to yellow 'you are here' 2. change the previous menu item back to its original state (eg red and "go here").

The 4 values "go here", "go there", "maybe here", "or even here" are the 4 values that should be assigned to the other menu items (like the example).

This is the code i already have:

$('#menu ul li.home').addClass('active')
$('#menu ul li.active a .go').html("you are here");

$("#menu ul li").click(function () { 
$('#menu ul li.active').removeClass('active');
     $(this).addClass('active');
     $('#menu ul li.active a .go').html("you are here");
  });

  var arr = [ "go here", "go there", "maybe here", "or even here" ];
  var obj = { item1: "go here", item2: "go there" ,item3: "maybe here", item4: "or even here"};

  $('#menu ul li').click(function () {
   var str = $('#menu ul li.active a .go').text();
   $('#menu ul li.active a .go').html(str);
  });

As you see, it's incomplete. I don't how to get the values from the array and assign them too a menu item. The replace text works, but not the change-back-to-original-state. Also, right now, for some reason i can't click ONTO the list item itself in order to activate the jquery code. I need to click just a few pixels under it. But i guess that's a css issue.

If anyone can help, i'd be super thankful!

Regards,

Mathijs

+2  A: 

This should work:

var msgs = [ "go here", "go there", "maybe here", "or even here" ];
var msgs_length = msgs.length;

$("#menu ul li").click(function () { 
     $('#menu ul li.active').removeClass('active');
     $(this).addClass('active');
     $('.go', this).text("you are here");

     $("#menu ul li").not(this).each(function(i) {
         $('.go', this).text(msgs[i % msgs_length]);
     });
});

Explanation:

  • Use text() instead of .html() if you want to set text only
  • $('.go', this) will find any element with class go inside the current element (read more about selector context)
  • $("#menu ul li").not(this) selects all li elements besides the current one (read more about .not())
  • i is the index of the element in the list of the selected elements (read more about .each())
  • i % msgs_length (modulo) ensures that you always have a valid index for the message array (in case there are more menu items than messages)

I don't know if the color thing already works, but this is only a CSS issue:

#menu ul li .go {
    color: #FF0;
}

#menu ul li.active .go {
    color: #F00;
}

Update:

Btw instead of "manually" setting the value for the home list entry:

$('#menu ul li.home').addClass('active');
$('#menu ul li.active a .go').html("you are here");

consider to simulate a click so that the values for the other list elements are correctly set:

$('#menu li.home').click();

Update2:

To fix the "have-to-click-below" issue ;)

$("#menu ul li a").click(function () { 
     $('#menu ul li.active').removeClass('active');
     $(this).parent().addClass('active');
     $('.go', this).text("you are here");

     $("#menu ul li a").not(this).each(function(i) {
         $('.go', this).text(msgs[i % msgs_length]);
     });
});
Felix Kling
Wow, that's perfect! That was fast, thanks :-)
Mathijs Delva
Mathijs Delva
@Mathijs Delva: What exactly is not working? What is the current behaviour?
Felix Kling
Right now, the click function on the list item doesn't get activated. I have to click just a few pixels beneath every list item in orde to activate the onclick event.
Mathijs Delva
Btw, the color works, that always worked :-) If you want to see what i'm talking about, click here: http://www.intra-interieur.be/www/
Mathijs Delva
@Mathijs Delva: Ah ok :) Well the clicking works for me (in Firefox) but the text does not change... :( Also I saw that you just added my code to your code. You can basically replace your code with mine.
Felix Kling
I think you misread the source code! My old code is in comment so i just use your code. As for the click in firefox: the click itself does work (the navigation works) but the click event for our code is not working. The color try it!
Mathijs Delva
@Mathijs Delva: Ah you are right, it was commented out ;) And now I also see the problem with the click. The problem is that the `a` element has the same height and width as the black area of the `li` element. So if you click on it, the click is performed on the `a` element and not on the `li`. But the actual `li` element is a bit higher then the black area that is why the click below it works. In order to make it work, I would just assign the click handler to the `a` tag instead of `li`. It should not be a problem to have multiple handlers assigned to one object.
Felix Kling
See my updated answer... (btw if you use Firefox, install Firebug to debug your HTML and JavaScript. It is really helpful, especially in order to see the dimensions of elements)
Felix Kling
Great, now it's perfect! :-) I use firebug, just didn't notice it! You're great man, thanks!
Mathijs Delva
@Mathijs Delva: You're welcome. If my answer helped you (I guess it did ;)) please click at the green tick beside it in order to mark this problem as "solved". (could be that you have to wait a bit before you can click it)
Felix Kling
Solved! Thanks again!
Mathijs Delva
A: 

This is the css

#menu {
position:  fixed;
top:  120px;
left:40px;
z-index: 40;
font-family: Arial, sans-serif;
font-size: 0.6em;
text-transform: uppercase;
}

#menu ul li {
    float:  right;
}

#menu ul li a{
    background-color: #292929;
    display: block;
    padding: 12px 6px 4px 6px;
    text-align: right;
    margin-bottom: 9px;
    color: #f5f5f5;
    text-decoration: none;
}

#menu ul li.active a .go{
    color: #e4d555;
}

#menu ul li a:hover .go{
    color: #e4d555;
}

#menu ul li a:hover{
    text-decoration: none;
}

#menu ul li .go{
    font-family: Georgia, Serif;
    color: #ff0000;
    text-transform: lowercase;
    font-size:11px;
}
Mathijs Delva
You should put this into your question (only the relevant part, just edit you question).
Felix Kling
Oh ok, sorry! Let's just keep talking in the original post :)
Mathijs Delva