Problem using variable within child selector in the hide effect
I'll skip my use case because it's a long and unnecessary story. Suffice it to say that I'm trying to avoid having a dedicated click function for each of the many 'more' links on my page. I think I can get down to one 'more' click function and one 'less' click function if I can pass the id of the parent div in as a variable.
I'm using the child selector (http://api.jquery.com/child-selector/) within a hide effect (http://jqueryui.com/demos/hide/). It works fine when I hard-code the id of the parent div. But when I pass the parent div name in as a variable it fails silently. I have tried every different syntax that I can come up with.
Please help.
Here's a simple working (non-working, really) test page I whipped up.
- <html>
- <head>
- <title>jQuery UI .hide test</title>
- <!-- I'm using the current jQuery and jQuery UI builds.
- Everything is enabled in my jQuery UI build -->
- <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
- <script type="text/javascript" src="js/jquery-ui-1.8.1.custom.min.js"></script>
- <script type="text/javascript" language="JavaScript">
- $(document).ready(function(){
- // click event to hide by hard-coded parentDiv name
- $("#direct").click(function () {
- $('#parentDiv > div.hideme').hide("blind", {}, 1000);
- return false;
- });
- // click event to hide by variable that contains the ParentDiv name
- $("#vari").click(function () {
- var hider = "#parentDiv";
- $('hider > div.hideme').hide("blind", {}, 1000);
- return false;
- });
-
- // click event to un-hide by hard-coded element name
- $("#unhide").click(function () {
- $("#foo").show("blind", {}, 1000);
- return false;
- });
- });
- </script>
- </head>
- <body>
- <div id="parentDiv">
- <p> Going to test hiding this element:</p>
- <div id="foo" class="hideme">Hide Me!</div>
- </div>
- <br><br>
- <a href="#" id="direct">This link is hard-coded to the parentDiv name.</a>
- <br>
- <a href="#" id="vari">This link uses a variable that contains the parentDiv name.</a>
- <br>
- <a href="#" id="unhide">This link unhides the div, again based on the hard-coded vid name.</a>
- </body>
- </html>
Test it and you will see that the links on lines 36 and 40 hide and unhide as expected, but the link on line 38 does not.
What am I missing?
Thanks in advance!