Richard Bland
Richard Bland

Reputation: 73

jQuery Conflicts?

I am installing the following script in a JS file which contains many others for a Wordpress blog. Could anyone point me in the direction of some tools to check for conflicts as this script doesn't work when I place it in the site build. (It works fine by itself).

Thank you...

this.randomtip = function(){
    var length = $("#message li").length;
    var ran = Math.floor(Math.random()*length) + 1;
    $("#message li:nth-child(" + ran + ")").show();
};

$(document).ready(function(){   
    randomtip();
});

Upvotes: 0

Views: 239

Answers (2)

Beetroot-Beetroot
Beetroot-Beetroot

Reputation: 18078

I don't think there's a conflict. It's just a question of scope.

If randomtip is within scope, then it can be called, for example:

$(document).ready(function(){   
    function randomtip(){
        var length = $("#message li").length;
        var ran = Math.floor(Math.random()*length) + 1;
        $("#message li:nth-child(" + ran + ")").show();
    };
    randomtip();
});

or

var MYNAMESPACE = (function() {
    var randomtip = function(){
        var length = $("#message li").length;
        var ran = Math.floor(Math.random()*length) + 1;
        $("#message li:nth-child(" + ran + ")").show();
    }
    ...
    return {
        randomtip: randomtip,
        ...
    };
})();

$(document).ready(function(){   
    MYNAMESPACE.randomtip();
});

Upvotes: 0

algorhythm
algorhythm

Reputation: 8728

try

var randomtip = function() {
    // [...]
};

// OR

function() randomtip {
    // [...]
}

// OR

window.randomtip = function() {
    // [...]
};

Upvotes: 1

Related Questions