[Quote Machine] call the function in jquery when the user click button

[Quote Machine] call the function in jquery when the user click button
0

#1

Hey camper, I have a simple problem… this is my code:

 $(function () {
  $("#button").click(function(){
    $("#message").myFunction();  // how to call a function here ? when the user click the button ? it should be retrunet in a element with #message selector
  });

So the question is simple - how to call myFunction in brackets { } ? In translate, I need this:
When the user click to the button “NEW QUOTE”, so it should call this function:

function  myFunction () {

    number =  Math.floor(Math.random() * (max - min + 1)) + min;

       if (number === 1) {
      return "Pokial nejsi prvy, tak si posledny";
    }

    else if (number === 2) {
      return "Nechce se ti do toho? Udelej to!";
    }

    else {
      return "Dej psovi jídlo a bude na tebe čumět celej život";
     }

}

and return some random quote to the element with #message selector
How? Your help can save 60 minutes + for me, so thank you very much.


#2

You will most likely need the .html() method here: $("#message").html("Some text"). You can replace "Some text" by the return of your function.


#3

Yes, but the return should be, after every click to the button, different… because it’s “random” quote machine… there can’t be just 1 quote.
Or how do you think it ? just write $("#message").html(“Norbit, don’t eat that cookies!”) ? So ?


#4

You can use myFunction() inside .html() to get a random quote each time.


#5

Ooh thanks so much !! I tried do this yeasterday and it didn’t work… It was because I was using Atom and Atom didn’t warned me, that I lost one bracket } …
I was searching for how do this for hours, trying a lot of ways, lol :smiley:

I’ll give you special thanks to my #100DaysOfCode log , in 4 hours :smiley: Should I link to your portfolio or what do you want for me ? :smiley: You really saved me a lot of time :smiley:


#6

Good luck with your #100DaysOfCode!

No need for a special thanks :slightly_smiling_face: