views:

88

answers:

5

Assuming I have the following two JQuery functions -

The first, which works:

$("#myLink_931").click(function ()
{
  $(".931").toggle();
});

and the second, which doesn't work:

$("#myLink_931").click(function ()
{
  var class_name = $(this).attr("id").split('_')[1];
  $("."+class_name).toggle();
});

I want to replace the first with the second, which is more generalizable, but can't find any obvious syntactical problem with the second which might be preventing it from working.

My guess is there's a problem with the syntax:

 "."+class_name

Is this bad syntax?

+3  A: 

This is what debuggers are for. Step through the code and make sure class_name is calculated as you expect. The debugger should let you view the result of "."+class_name as well.

Frank Schwieterman
What debugger would you suggest? I don't have one set up.
ipso facto
Firebug is a Firefox extension that is essential to web development and javascript debugging.
RedWolves
+1  A: 

Class names and IDs aren't allowed to start with numbers - doesn't explain why one works and the other doesn't though. Give us a bit more info as above.

Greg
Why the first works?
Daniel Moura
while this is true by spec, most browsers (ie, firefox) let you get away with it
mkoryak
If you're right that would explain why it doesn't work the second way but not why it does work the first way?
ipso facto
+3  A: 

I created a sample page and dropped your example code in and it worked as expected. Perhaps there is another issue on the page? Can you post a link to the actual site?

Here is the code I used:

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
    <script src="scripts/script.js" type="text/javascript"></script>
</head>
<body>
    <div id="myLink_931">Click Me</div>
  <div class="931">HI</div>
</body>
</html>

and the script file:

(function($) {
    $(document).ready(function() {
        $("#myLink_931").click(function() {
            var class_name = $(this).attr("id").split('_')[1];
            $("." + class_name).toggle();
        });
    });
})(jQuery);
RedWolves
Thanks. This is in a dev area, though, can't provide a link. Which browser are you using?
ipso facto
Firefox 3.5, IE6, IE7, IE8, Google Chrome.
RedWolves
+3  A: 

They work the same.

Working Demo

Russ Cam
Wow. Thanks .
ipso facto
I would take Greg's advice though and not begin your class names or ids with numbers - technically it's not allowed in the HTML specification, but I'm yet to see a browser that doesn't cope with it. Has anyone?
Russ Cam
+1  A: 

Is it possible you're not wrapping your 2nd example in the ready syntax [i.e. $(function(){ })] which would mean that the elements haven't been created in the DOM yet?

John Rasch
No, everything is wrapped inside 'ready'. Thanks, though.
ipso facto