0

How I can call/trigger a jQuery function (which is in another file) from JS. So basically this is the jQuery function (/js/animation.js).

$('#spin').click(function () {
    var tickets = Math.floor(Math.random() * 200) + 1;
    if(tickets < 51) {  
    var winner = 155;
    }
    else {
    var winner = 145;   
    }
    rouletteSpin(winner);
});

I know that its based on a codepen Roulette Animation. Now the problem is that it should not roll without going through an if statement. The statement should be a variable using Database info. That's the point. In my opinion you cant use PHP / select DB Info in jQuery so I decided to go into my index.php to trigger the jQuery function after the if statement in js went through. So let me show you my HTML Button + JS Function:

 onclick="openCase()

into the first HTML Button tag.

Now the JS Function:

    <script>    
    var credits = CodeToGet_MYSQL_DB_Info_here_written_in_php;


    function openCase() {
    if(credits > 10000) {
     spinIt();
    }
    else {
        alert('Something went wrong');
    }
    }

And changed the jQuery Function (in path /js/animation.js) to the following:

function spinIt () {
    var tickets = Math.floor(Math.random() * 200) + 1;
    if(tickets < 51) {  
    var winner = 155;
    }
    else {
    var winner = 145;   
    }
    rouletteSpin(winner);
});

That is not working. I already googled a lot so please don't flame me. Already used pretty much every method of this post:

calling Jquery function from javascript

Would be nice if someone can help me out!

6
  • 3
    Why is “Can someone help me?” not an actual question? Commented Apr 24, 2017 at 17:53
  • load the js file first which call the function then the second which have the actual function. Commented Apr 24, 2017 at 17:53
  • 1
    "In my opinion you cant use PHP / select DB Info in jQuery" Yes you can, by using AJAX. Commented Apr 24, 2017 at 17:54
  • @JayBlanchard Read the whole text and you should understand my question. "How can I call a jQery function (which is in another file) in JS?" Commented Apr 24, 2017 at 17:58
  • Read it. Edited it. Nope, still don't understand the question and others agree with me. Commented Apr 24, 2017 at 17:59

0

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.