show when clicked and then hide when clicked again

Juliver Galleto

how to bind same functions but different arguments given?

I'm making a dropdown stuff and I'm trying to hide the submenu when parent is click again (refer below)

when click then show submenu and then when click again and the submenu is displayed already then hide it.o

What I'm currently tried so far (refer below)

jquery

$( ".has_sub" ).bind({
    click: function() { //when click and sub is not displayed yet
        $(this).find('.sub').slideDown();
    }, click: function() { //when click and sub is displayed already
        $(this).find('.sub').hide();
    }
});

html

<ul>
    <li class="has_sub">
        <a href="#">Menu 1</a>
        <ul class="sub">
            <li>sub menu 1</li>
            <li>sub menu 2</li>
        </ul>
    </li>
</ul>
Arun P Johny

In your case since you have used the same key twice, the object will have only one property with the value as the value of the last item, so only one handler - the one that hides will get registered.

Since you want to toggle the display with slide animation you can use slideToggle()

$(".has_sub").on('click', function() {
  $(this).find('.sub').stop().slideToggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
  <li class="has_sub">
    <a href="#">Menu 1</a>
    <ul class="sub">
      <li>sub menu 1</li>
      <li>sub menu 2</li>
    </ul>
  </li>
</ul>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

show element when clicked and hide again when not active

From Dev

jQuery Toggle hide when clicked again

From Dev

jQuery show content when clicked hide initially

From Dev

Show/Hide Div when link is clicked

From Dev

Show/hide li icon when clicked on with jQuery

From Dev

hide and show button when check box clicked

From Dev

Dynamically hide/show textboxs when a checkbox clicked

From Dev

hide / show option when radio button is clicked

From Dev

Show div when enter the site, but hide when li is clicked

From Dev

Hide div when the page is loaded and show when link is clicked

From Dev

Show menu when clicked

From Dev

jQuery - show edit textarea form when link is clicked and hide paragraph

From Dev

Show/Hide button loses styling on some browsers when clicked

From Dev

Show and hide active class in li when clicked on input button

From Dev

Show div once clicked and hide when clicking outside it

From Dev

show/hide textarea with validation.when displayed clicked on radio button

From Dev

AS3 Show and hide movieclip/image when button/mc is clicked

From Dev

How to show a specific div when a link is clicked and hide others?

From Dev

Hide a component when clicked outside

From Dev

hide content when div clicked

From Dev

when second parent menu is clicked show its submenu and hide the submenu of previously clicked parent menu

From Dev

HTML subsubmenu show when clicked

From Dev

Show DialogFragment when Spinner is clicked

From Dev

Show ArrayAdapter when TextView is clicked

From Dev

Toggle div when clicked and hide when clicked outside

From Dev

Toggle div when clicked and hide when clicked outside

From Dev

javascript autoclick loop not resetting when clicked again

From Dev

how to make an object minimise again when clicked

From Dev

Show chart if button clicked, close it if clicked again

Related Related

  1. 1

    show element when clicked and hide again when not active

  2. 2

    jQuery Toggle hide when clicked again

  3. 3

    jQuery show content when clicked hide initially

  4. 4

    Show/Hide Div when link is clicked

  5. 5

    Show/hide li icon when clicked on with jQuery

  6. 6

    hide and show button when check box clicked

  7. 7

    Dynamically hide/show textboxs when a checkbox clicked

  8. 8

    hide / show option when radio button is clicked

  9. 9

    Show div when enter the site, but hide when li is clicked

  10. 10

    Hide div when the page is loaded and show when link is clicked

  11. 11

    Show menu when clicked

  12. 12

    jQuery - show edit textarea form when link is clicked and hide paragraph

  13. 13

    Show/Hide button loses styling on some browsers when clicked

  14. 14

    Show and hide active class in li when clicked on input button

  15. 15

    Show div once clicked and hide when clicking outside it

  16. 16

    show/hide textarea with validation.when displayed clicked on radio button

  17. 17

    AS3 Show and hide movieclip/image when button/mc is clicked

  18. 18

    How to show a specific div when a link is clicked and hide others?

  19. 19

    Hide a component when clicked outside

  20. 20

    hide content when div clicked

  21. 21

    when second parent menu is clicked show its submenu and hide the submenu of previously clicked parent menu

  22. 22

    HTML subsubmenu show when clicked

  23. 23

    Show DialogFragment when Spinner is clicked

  24. 24

    Show ArrayAdapter when TextView is clicked

  25. 25

    Toggle div when clicked and hide when clicked outside

  26. 26

    Toggle div when clicked and hide when clicked outside

  27. 27

    javascript autoclick loop not resetting when clicked again

  28. 28

    how to make an object minimise again when clicked

  29. 29

    Show chart if button clicked, close it if clicked again

HotTag

Archive