views:

61

answers:

3

will we have to replace every $ with jquery?

$(document).ready(function() {

    $('.tabs a').click(function(){
        switch_tabs($(this));
    });

    switch_tabs($('.defaulttab'));

});

function switch_tabs(obj)
{
    $('.tab-content').hide();
    $('.tabs a').removeClass("selected");
    var id = obj.attr("rel");

    $('#'+id).show();
    obj.addClass("selected");
}
+2  A: 

As long as you are sure the snippet contains just jQuery usage of $ then you can wrap it in a closure

(function($){
    $(document).ready(function() {

        $('.tabs a').click(function(){
            switch_tabs($(this));
        });

        switch_tabs($('.defaulttab'));

    });

    function switch_tabs(obj)
    {
        $('.tab-content').hide();
        $('.tabs a').removeClass("selected");
        var id = obj.attr("rel");

        $('#'+id).show();
        obj.addClass("selected");
    }
})(jQuery);
Simon
but how to sure about this "snippet contains just jQuery usage of $"
metal-gear-solid
+1  A: 

use

$.noConflict();


(function($) { 
  $(function() {
    // more code using $ as alias to jQuery
  });
})(jQuery);
// other code using $ as an alias to the other library

Resolve the conflict issues

More detail : http://api.jquery.com/jQuery.noConflict/

Pranay Rana
A: 
var $j = jQuery.noConflict();


$j(document).ready(function() {

$j('.tabs a').click(function(){
    switch_tabs($j(this));
});

switch_tabs($j('.defaulttab'));

});

function switch_tabs(obj)
{
$j('.tab-content').hide();
$j('.tabs a').removeClass("selected");
var id = obj.attr("rel");

$j('#'+id).show();
obj.addClass("selected");
}
adardesign