Jquery function to show hide elements

Hello,

i am new to jquery.
i want to modify the below function, so it will accept 2 element names dynamically (in place of #div1, .btn).

$("#div1").click(function(){
  $(".btn").show();
});

:slight_smile:

Hi,

I’m not sure I quite understand what you are getting at.

The code you posted attaches an event handler to the element with an id of “div1”', so that when you click on it any elements with a class of “btn” are shown (have their display property set accordingly).

Could you describe in a little more detail what you are trying to do.
If you could post a snippet of HTML that would be great.

Or are you trying to write a function, which does what I describe above, which you can call thus:
attachHandler(el1, el2)

Where el1 is the element to click on and el2 is the element to hide.

absolutly i need the syntax for this !

This’ll work:

<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Unbenanntes Dokument</title>
    <style>
      .btn1, .btn2{ display: none; color:red;}
    </style>
  </head>

  <body>
    <div id="div1">Click me</div>
    <div class="btn1">Hello!</div>

    <div id="div2">Click me, too</div>
    <div class="btn2">Hello again!</div>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
      function attachShow(el1, el2){
        $(el1).click(function(){
          $(el2).show();
        });
      }

      attachShow("#div1", ".btn1");
      attachShow("#div2", ".btn2");
    </script>
  </body>
</html>

Excellent example!!! Thanks pullo!!