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!

Community
  • 1
  • 1
Lukeyyy
  • 25
  • 3
  • 3
    [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – Jay Blanchard Apr 24 '17 at 17:53
  • load the js file first which call the function then the second which have the actual function. – sumit chauhan Apr 24 '17 at 17:53
  • 1
    *"In my opinion you cant use PHP / select DB Info in jQuery"* Yes you can, by using AJAX. – Jay Blanchard Apr 24 '17 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?" – Lukeyyy Apr 24 '17 at 17:58
  • Read it. Edited it. Nope, still don't understand the question and others agree with me. – Jay Blanchard Apr 24 '17 at 17:59
  • Can you add the code that you are using to retrieve the information from the database? I would start debugging there, make sure that `credits` is actually returning a result as expected and not an undefined variable. – jon3laze Apr 26 '17 at 19:35

0 Answers0