1

I recently solved a jquery problem I had by switching all the '$'s in my code to 'jQuery'

I'm trying to use this code:

var $items = $('#vtab>ul>li');
$items.mouseover(function() {
    $items.removeClass('selected');
    $(this).addClass('selected');

    var index = $items.index($(this));
    $('#vtab>div').hide().eq(index).show();
}).eq(1).mouseover();

but am not sure how to change the $ signs out for $items. Is it as simple as jQuery.items? Is this even jQuery code? I'm confused.

Thanks, Zeem

3 Answers 3

4

$items does not need to be changed. jQuery's variable is (typically) just a $, whereas $items is it's own variable name.

So your code would become:

var $items = jQuery('#vtab>ul>li');
$items.mouseover(function() {
    $items.removeClass('selected');
    jQuery(this).addClass('selected');

    var index = $items.index(jQuery(this));
    jQuery('#vtab>div').hide().eq(index).show();
}).eq(1).mouseover();

Edit You could also use a self-invoking function like so:

(function($){
    var $items = $('#vtab>ul>li');
    $items.mouseover(function() {
        $items.removeClass('selected');
        $(this).addClass('selected');

        var index = $items.index($(this));
        $('#vtab>div').hide().eq(index).show();
    }).eq(1).mouseover();
})(jQuery);

This will allow you to use the $ variable in your code BUT you will not be able to use the variables within, outside of the function so be careful.

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to replace $items at all.

$items = jQuery('#vtab>ul>li'); 

will work.

$items is a variable name and is not part of the $ namespace.

Comments

1

var $items is just a variable. Prefixing variables with $ lets the developer know that it's a jQuery object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.