views:

366

answers:

3

Hi,

I have menu designed using jQuery in oracle apex.The menu is like this

Parent1
    child1
    child2
parent2
    child3
    child4
parent3
    child5
    child6

My problem is when I click on parent1 only child1 and child2 should display but in my case each parent gets expanded. And the user can see all the childs. Which should not happen users should only be able to see the clicked parent and child details.

My code is as follows.

$(document).ready(function() {
    var msg = false;
    $('.dropdown_menu').click(function() {
        msg = !msg;
        if (msg)
            $(this).parents('.parent_menu').find('.child_menu').slideDown("slow");
        else
            $('.child_menu').hide();
        return false;
    });
});
A: 

Try:

$(document).ready(
    function(){ 
      var msg = false; 
      $('.child_menu').click(
         function() { 
           msg = !msg; 
           if (msg) 
             //I have only ONE parent. Pick him, and all his children.
             $(this).parent().find('.child_menu').slideDown("slow"); 
           else 
             $('.child_menu').hide(); 
           return false; 
          }
       ); 
     }
);
Alex Bagnolini
Hi Alex,Thanks for the reply but it's not letting my child page to reload it as i am showing attributes of child on rightside when clicked on child.With your suggestin it only stops alll the child to expand but the reloading of page is not happening which is not required.
Bhavesh
A: 

Just change parents to the singular parent. While both find the ancesters of the element, the scope is different.

.parent() searches the immediate parent only.

.parents() finds all parents up to the base of the DOM tree.

EDIT1: I must be dumb, while the above is true, you indicate to click the PARENT of the elements, not the children - now that I have my glasses on:

.children() would be the correct selector of that (from the parent of the children of course, as you indicated.

so $(this).children(). code to do what you want here.

change:

 $(this).parents('.parent_menu').find('.child_menu').slideDown("slow");

This will select the .parent_menu items children from my parent, which says "all .parent_menu items and all .child_menu within that.

to

$(this).children().slideDown("slow");

This select only MY (this) immediate children. IF you happen to have other children (other classes) you can specify it futher with:

$(this).children('.child_menu').slideDown("slow");

Edit2:one side note: it is unclear if the class you have the .click event on is applied at each parent (parent1, parent2 etc) or to the parent of parent1, parent2 etc. which would change the scope of the .click event capture, I make the assumption that the class is applied at the parent1, parent2 etc level.

Mark Schultheiss
Hi Mark,Thanks for the suggestion.After I tried your suggestion the parent is not expanding.My concern is that when i click on child the page gets refreshed and all the child under every parents gets expanded so how can i save the last state when page gets refreshed using jquery.Kindly suggest.Thanks in Advance.
Bhavesh
Hi Mark,View source gives me this<div id="menu"><div class="parent_menu"> <a class="dropdown_menu" href="f:P17_LOCATION_ID:8">Belgium</a><div class="child_menu"><li> <a href="f:NO::P17_LOCATION_ID:17">Ch1</a> </li><li> <a href="f:NO::P17_LOCATION_ID:27">Ch2</a> </li></div></div><div class="parent_menu"> <a class="dropdown_menu" href="f?p=102:17:100173801651673::NO::P17_LOCATION_ID:35">Germany</a><div class="child_menu"><li> <a href="f?p=102:17::NO::P17_LOCATION_ID:36">Ch3</a> </li><li> <a href="f?p=102:17:NO::P17_LOCATION_ID:37">Ch4</a> </li></div></div></div>
Bhavesh
A: 

It would have helped a great deal if you would have provided some HTML. So, I'm assuming you are using this HTML Sorry I see the comment where you did provide some.

This script should work with the HTML you provided:

$(document).ready(function(){
 // disable links in the menu
 $('#menu a').click(function(){
  return false
 });
 // hide child menus
 $('.child_menu').hide();
 // enable accordian menu
 $('.dropdown_menu').click(function() {
  $(this).parent().find('.child_menu').toggle("slow");
  return false;
 });
});


Edit: I made the following changes:

  • I only disabled the dropdown_menu links, so the child_menu links will now work
  • To make the other child menus close when clicking on a new parent, I added an "active" class, then just hid that when you click.
  • The script now looks at the window hash for the location ID from the child menu to determine which child menu to display. I don't know how you are changing your child menu links (I couldn't just go in and add the id to this: "f:NO::P17_LOCATION_ID:17"), but somehow you need to add the location id ( like #17) to the end of the link: http://www.mysite.com/somepage.htm#17. The script will take this URL and find the location ID is 17 and open the Belgium child menu when it finds the ID at the end of the href (see this jQuery reference on the selector I used).
  • Edit#2: Forgot to prevent clicking the parent from hiding the child as you requested, so now only clicking another parent will hide the child.

Using this HTML (you provided in a comment below):

<div id="menu">
 <div class="parent_menu">
  <a class="dropdown_menu" href="f:P17_LOCATION_ID:8">Belgium</a>
  <div class="child_menu">
   <li>
    <a href="f:NO::P17_LOCATION_ID:17">Ch1</a>
   </li>
   <li>
    <a href="f:NO::P17_LOCATION_ID:27">Ch2</a>
   </li>
  </div>
 </div>

 <div class="parent_menu">
  <a class="dropdown_menu" href="f?p=102:17:100173801651673::NO::P17_LOCATION_ID:35">Germany</a>
  <div class="child_menu">
   <li>
    <a href="f?p=102:17::NO::P17_LOCATION_ID:36">Ch3</a>
   </li>
   <li>
    <a href="f?p=102:17:NO::P17_LOCATION_ID:37">Ch4</a>
   </li>
  </div>
 </div>

</div>

With this script:

$(document).ready(function(){
 // disable accordion links in the menu
 $('.dropdown_menu a').click(function(){
  return false
 });
 // hide child menus
 $('.child_menu').hide();
 // get current location ID from window hash
 var hid = window.location.hash;
 hid = hid.substring(1,hid.length);
 // find and open child menu that matches the ID
 $('.child_menu a[href$="' + hid + '"]').closest('.child_menu').addClass('active').slideDown("slow");
 // enable accordian menu
 $('.dropdown_menu').click(function() {
  // prevent hiding the child menu if it's already open
  if ($(this).parent().find('.child_menu').hasClass('.active')) return false;
  // find previously active menu and close it and open current one
  $('.active').removeClass('active').slideUp("slow");
  $(this).parent().find('.child_menu').addClass('active').toggle("slow");
  return false;
 });
});

Lastly, the script you just added in the comments, I can't do anything with that unless you provide the HTML as well.

fudgey
Hi Fudgy,Thanks for te reply.I tried your suggestion but now the child gets hidden on everyclick to parent which is not desired.What i want is when I click on any child menu the other child shud not expand.Currently on clicking child menu the url is made dyanamically using plsql procedure and which in turn refreshes the page and loads the corresopnding attributes of childkindly help it is really very urgent for me.
Bhavesh
I'm a bit confused. Do you want the child menu to not get hidden again once it's been shown? "the other child should not expand" is also confusing to me, do you want the other child menus to hide then? Do you know about the accordian menu (http://jquery.bassistance.de/accordion/demo/)? Is there an example on that page that does what you want? And lastly, if the page gets refreshed the menu will reset, are you saying you want it to stay open on that child menu to show new information?
fudgey
Hi Fudgey,yes u got it right in end lines.When page gets refreshed on child menu click what i want is the parent which was lastly expanded that shud only be expanded and rest parents shud not show the child whereas now i am getting all the child menus expnded on page refresh which is not desired.Kindly help me as this is urgent and i am new to java.Your help will be appriciated.
Bhavesh
Hi fudgey thanks accordion is the answer for me can u please help me with this one<script>$(document).ready(function(){$("dd:not(:first)").hide();$("dt a").click(function(){$("dd:visible").slideUp("slow");$(this).parent().next().slideDown("slow");return false;});});</script>here but there is one problem on page refresh the first parent menu is expanded but i what when page refreshes the same parent menu shud oopen where i have clicked last time before the page refreshes.say i have 234as 4 parent menu then if i click on 3rd one the next time when page refreshes is shud se3
Bhavesh
The answer above has been updated.
fudgey
Hi Fudgey thanks for the answer my only query is I am having jquery only and no accordion plugin with me will your code work without accordion plugin
Bhavesh
Hi Fudgey Bingo!Thanks a lot man you savd me.Just tell me how to accept answer
Bhavesh
You already did :)
fudgey
Hi Fudgey i have done this for my code<script> $(document).ready(function(){ var hid = ($('#P24_LOCATION_ID').val()); $('.child_menu').hide(); $('.child_menu a[href$="' + hid + '"]').closest('.child_menu').addClass('active').slideDown("slow"); $('.dropdown_menu').click(function(event) { if ($(this).parent().find('.child_menu').hasClass('.active')) return false; $('.active').removeClass('active').slideUp("slow"); $(this).parent().find('.child_menu').addClass('active').toggle("slow"); return false; }); });</script>But now my parent does not workon click can u figureout
Bhavesh