Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: variable as ID selector not working

Ok here is my code:

$tabularID = 0;
$('a.swfselector').live('mouseover', function(event) {
            $tabularID= $(this).parent().attr('id');
            $(this).parent().children().not(this).find('.tab').each(function() {
                      $(this).fadeTo(100, 0.4)
            })
            $(this).find('.tab').each(function() {
                      $(this).fadeTo(100,1)
            })

    });
$('#' + $tabularID).live('mouseleave', function(event) {
            alert($tabularID);
            $(this).children().find('.tab').each(function() {
                      $(this).fadeTo(100,1)
            })

    });

Jquery doesn't like this selector:

$('#' + $tabularID)

Although if I change it to:

$('#27')

It alerts my variable $tabularID just fine, so I know it isn't the variable that is wrong (Output of $tabularID is 27). I need a variable here because the parent ID changes depending on which they mouseover.

Anyone can see what I can't? probably really obvious.

like image 919
George Reith Avatar asked Jan 19 '23 21:01

George Reith


2 Answers

Your ID must begin with a letter a-z or A-Z.

This code $('#' + $tabularID) is only affected at the first time you run it. It means your $tabularID = 0.

When you mouse over it only update the $tabularID value, but it will not update the binding to event of this object $('#' + $tabularID)

I think you can change your code like this:

$tabularID = 0;
$('a.swfselector').live('mouseover', function(event) {
            $tabularID= $(this).parent().attr('id');
            $(this).parent().children().not(this).find('.tab').each(function() {
                      $(this).fadeTo(100, 0.4)
            })
            $(this).find('.tab').each(function() {
                      $(this).fadeTo(100,1)
            })

            $('#' + $tabularID).live('mouseleave', function(event) {
                alert($tabularID);
                $(this).children().find('.tab').each(function() {
                      $(this).fadeTo(100,1)
                })

            });

    });
like image 195
Khoa Nguyen Avatar answered Jan 28 '23 07:01

Khoa Nguyen


I often use variables in selectors. And all works fine for me this way. Just avoid using IDs like '123'. ID naming rules:

  • Must begin with a letter A-Z or a-z
  • Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")
  • Values are case-sensitive
like image 35
CoolEsh Avatar answered Jan 28 '23 09:01

CoolEsh