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).
Code:$("#div1").click(function(){ $(".btn").show(); });![]()
| SitePoint Sponsor |
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).
Code:$("#div1").click(function(){ $(".btn").show(); });![]()

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.
Start a blog, they said. People will read it, they said.

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.
Start a blog, they said. People will read it, they said.

This'll work:
HTML Code:<!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>
Start a blog, they said. People will read it, they said.
Bookmarks