views:

57

answers:

4

Hello! I am very new to javascript and ajax/jquery and have been working on trying to get a script to open and close the drop menu on click rather that hover.

The menu in question is found on http://www.gamefriction.com/Coded/ and is the dark menu on the right side under the header. I would like it to open and close like the other menu that is further below it (it is light gray and is in the "Select Division" module).

The gray menu is part of a menu and the language menu is not.

I have a jquery import as well which can be found in the view source of the above link.

My Javascript Code:

<script type="text/javascript">

 /* Language Selector */

 $(function() {
     $("#lang-selector li").hover(function() {
         $('ul:first',this).css('display', 'block');
     }, function() {
         $('ul:first',this).css('display', 'none');
     });
 });

 $(document).ready(function(){ 

  /* Navigation */
  $('.subnav-game').hide();
  $('.subnav-game:eq(0)').show();
  $('.preorder-type').hide();


  $('.preorder-type:eq(3)').show();


 });


 </script>

My CSS:

#lang-selector 
  {
  font-size: 11px;
  height: 21px;
  margin: 7px auto 17px auto;
  width: 186px;
  }

 #lang-selector span 
  {
  color: #999;
  float: left;
  margin: 4px 0 0 87px;
  padding-right: 4px;
  text-align: right;
  }

 #lang-selector ul 
  {
  float: left;
  list-style: none;
  margin: 0;
  padding: 0;
  }

 #lang-selector ul li a 
  {
  padding: 3px 10px 1px 10px;
  }

 #lang-selector ul, #lang-selector a 
  {
  width: 186px;
  }

 #lang-selector ul ul 
  {
  display: none;
  position: absolute;
  }

 #lang-selector ul ul li
  {
  border-top: 1px solid #666;
  float: left;
  position: relative;
  }

 #lang-selector a 
  {
  background: url("http://www.gamefriction.com/Coded/images/language_bg.png") no-repeat;
  color: #666;
  display: block;
  font-size: 10px;
  height: 17px;
  padding: 4px 10px 0 10px;
  text-align: left;
  text-decoration: none;
  width: 166px;
  }

 #lang-selector ul ul li a 
  {
  background: #333;
  color: #999;
  }

 #lang-selector ul ul li a:hover 
  {
  background: #c4262c;
  color: #fff;
  }

My HTML:

<div id="lang-selector">
      <ul>
       <li>
        <a href="#">Choose a Language</a>
        <ul>
         <li><a href="?iw_lang=en">English</a></li>
         <li><a href="?iw_lang=de">Deutsch</a></li>
         <li><a href="?iw_lang=es">Espa&ntilde;ol</a></li>
         <li><a href="?iw_lang=fr">Fran&ccedil;ais</a></li>
         <li><a href="?iw_lang=it">Italiano</a></li>
        </ul>
       </li>
      </ul> 
     </div>

Thanks!

+1  A: 

search this $("#lang-selector li").hover and replace with

$("#lang-selector li").click

sushil bharwani
I have tried that and while it works to make it open, you can not close the dropdown after it has been opened. You have to refresh the page to get rid of the dropdown.
TankDriver
This won't work because hover() accepts two functions while click only accepts one.
RussellUresti
A: 

.hover, .click, .something, are all triggers, view this link:

Jquery Events

to learn more about events in Jquery!

Ps: sushil bharwani (vote it), is right, just change your .hover by the .click

Zuul
No, changing it won't work, hover() accepts two functions (in and out) while click() only accepts one.
RussellUresti
Well, as RobertPitt said, use the toggle handler!With click, user must click to open, and must click to close... it's way deferent from hover...
Zuul
+2  A: 
 $(function() {
     $("#lang-selector li:first").click(function(){
         $('ul:first',this).toggle();
     })
 });

Using toggle will require you to click to open then reclick to close

RobertPitt
This worked perfectly so far. Thanks a ton! When I tried it last night right after you originally posted it, I could not get it to work.
TankDriver
Glad it helped you, Just remember to that the jQuery documentation is extreamly simple and you will always find a good answer there as well.
RobertPitt
+1  A: 

I would do something like this...

$(function() {
 $("#lang-selector > li").click(function() {
     $('ul:first',this).toggleClass('active');
 });
});

And, then, in the CSS add this:

.active { display: block; }

<< EDIT: Removed "ul" from ".active" class for CSS rendering efficiency >>

Also make sure that the sub-nav <ul> has "display: none;" on it by default in your CSS.

This will make it so that clicking an <li> tag in #lang-selector, but not in any sub-nav <ul> tags will either open or close the sub-nav, depending on it's current state.

If you're worried about the accessibility of having "display: none" on the sub-nav by default, you can do something like this...

 $(function() {
  $("#lang-selector li ul").addClass("hidden");
  $("#lang-selector li").click(function(e) {
      e.preventDefault();
      $('ul:first',$(this)).toggleClass('hidden active');
    });
 });

<< EDIT: Altered selectors to match example provided, turned "this" into jQuery object. >>

And then also add this to the CSS

.hidden { display: none; }

In this scenario, you have the <ul> showing by default, then when the document loads, jQuery adds the "hidden" class to all of them to hide them, then on the click of the <li> it will toggle the hidden and active classes, displaying them or hiding them.

You'll also need to remove your current "display: none" from your #lang-selector ul ul in CSS, otherwise it takes priority over the hidden / active classes.

RussellUresti
Could not seem to get either of these methods to even display the menu on clicking.
TankDriver
Well, you can't just copy and paste this, you have to also alter your own code... Here's a link that shows it working. http://jsbin.com/irota/2/edit
RussellUresti
I see now. I really have no knowledge of javascript or jQuery yet as this is my first time ever using it on a site. When I tried this yesterday it would not even display the dark image to click.
TankDriver