JavaScript How can I run a script again without refreshing the whole page?

If you look at greenb.byethost12.com you will see what I have developed so far. As of now, when you click on the correct answer, it document.location.reload(index.php) The whole page refreshes.

What I want is that just the content refreshes with a new set of numbers (run only the JavaScript).

Not sure if I'm making myself clear but any help will be gladly appreciated. Also any critiques are welcome since I might be making TON of errors that I haven't even seen yet. Cheers!

Code:

<!DOCTYPE html>

MathMap

    <div data-role="page" id="page1">
    <div data-theme="e" data-role="header">
    <a id="score" data-role="button" data-transition="none" target='_blank' href="#page1" data-icon="star"
    data-iconpos="left" class="ui-btn-right">
        Score
    </a>
    <h1>
        MathMap&trade;      
    </h1>
    </div>
<div align="center" data-role="content">
<div align="center"><h1 id="equal">

    </h1></div>
<div id="main" align="center" style="top: 150px;">
    <a id="a" data-role="button" data-theme="e" target='_blank' href="#page1" data-inline="true">

    </a>
    <a id="b" data-role="button" data-theme="e" target='_blank' href="#page1" data-inline="true">

    </a>
    <a id="c" data-role="button" data-theme="e" target='_blank' href="#page1" data-inline="true">

    </a>
    <a id="d" data-role="button" data-theme="e" target='_blank' href="#page1" data-inline="true">

    </a></div>
   <script>
     $(document).ready(function(){
     $("#main").hide().fadeIn(1000);
     });

     var x = Math.floor(Math.random()* 50);
     var y = Math.floor(Math.random()* 50);
     var a = Math.floor(Math.random()* 10);
     var b = Math.floor(Math.random()* 10);
     var c = Math.floor(Math.random()* 10);
     var d = Math.floor(Math.random()* 10);
     var divide =" \u00F7 "



     var problems = new Array();
     problems[0]=x+y;
     problems[1]=x-y;
     problems[2]=x*y;
     problems[3]=x/y;

     var split = new Array();
     split[0]=$("#a");
     split[1]=$("#b");
     split[2]=$("#c");
     split[3]=$("#d");

     var formula = new Array();
     formula[0] = x +" + "+ y +" = ";
     formula[1] = x +" - "+ y +" = ";
     formula[2] = x +" x "+ y +" = ";
     formula[3] = x + divide + y +" = ";

     var formrand = formula[Math.floor(Math.random()*formula.length)];
     var prorand = problems[Math.floor(Math.random()*problems.length)];
     var rand = split[Math.floor(Math.random()*split.length)];

        $("#equal").text(function myCalculation(){

              if ((formrand) == (formula[0])){
                $("#equal").text(formula[0]);
                $("#a").text(problems[0]+a);
                $("#b").text(problems[0]+b);
                $("#c").text(problems[0]+c);
                $("#d").text(problems[0]+d);
                $(rand).text(problems[0]);
                $("#a,#b,#c,#d").not(rand).click(function(){
                    $(this).transition({ opacity: 0.2, scale: 0.5});
                });
                $(rand).click(function(){
                $("#equal").text(formrand+problems[0]);
                $(rand).transition({ scale: 1.5 }).animate({"background-color":"green","color":"white"},1000);
                $("#a,#b,#c,#d").not(rand).transition({ opacity: 0.2, scale: 0.5 }).delay(2000,function(){
                    window.location.reload("index.php");
                });

                });


            }

               if ((formrand) == (formula[1])){
                $("#equal").text(formula[1]);
                $("#a").text(problems[1]+a);
                $("#b").text(problems[1]+b);
                $("#c").text(problems[1]+c);
                $("#d").text(problems[1]+d);
                $(rand).text(problems[1]);
                $("#a,#b,#c,#d").not(rand).click(function(){
                    $(this).transition({ opacity: 0.2, scale: 0.5});
                });
                $(rand).click(function(){
                $("#equal").text(formrand+problems[1]);
                $(rand).transition({ scale: 1.5 }).animate({"background-color":"green","color":"white"},1000);
                $("#a,#b,#c,#d").not(rand).transition({ opacity: 0.2, scale: 0.5 }).delay(2000,function(){
                    window.location.reload("index.php");
                });

                });

            }
              if ((formrand) == (formula[2])){
                $("#equal").text(formula[2]);
                $("#a").text(problems[2]+a);
                $("#b").text(problems[2]+b);
                $("#c").text(problems[2]+c);
                $("#d").text(problems[2]+d);
                $(rand).text(problems[2]);
                $("#a,#b,#c,#d").not(rand).click(function(){
                    $(this).transition({ opacity: 0.2, scale: 0.5});
                });
                $(rand).click(function(){
                $("#equal").text(formrand+problems[2]);
                $(rand).transition({ scale: 1.5 }).animate({"background-color":"green","color":"white"},1000);
                $("#a,#b,#c,#d").not(rand).transition({ opacity: 0.2, scale: 0.5 }).delay(2000,function(){
                    window.location.reload("index.php");
                });

                });

            }
              if ((formrand) == (formula[3])){
                $("#equal").text(formula[3]);
                $("#a").text(Math.round(problems[3]+a));
                $("#b").text(Math.round(problems[3]+b));
                $("#c").text(Math.round(problems[3]+c));
                $("#d").text(Math.round(problems[3]+d));
                $(rand).text(Math.round(problems[3]));
                $("#a,#b,#c,#d").not(rand).click(function(){
                    $(this).transition({ opacity: 0.2, scale: 0.5});
                });
                $(rand).click(function(){
                $("#equal").text(formrand+Math.round(problems[3]));
                $(rand).transition({ scale: 1.5 }).animate({"background-color":"green","color":"white"},1000);
                $("#a,#b,#c,#d").not(rand).transition({ opacity: 0.2, scale: 0.5 }).delay(2000,function(){
                    window.location.reload("index.php");
                });

                });
            }
        });






    </script>
</div>
<div data-theme="e" data-role="footer" data-position="fixed" data-inline="true">
    <h5>
        MathMap Learning
    </h5>
</div>
 </body>

Answer:1

Try using callbacks Or you can use functional loops where you end up calling the same functions again after an event occurs like trendy suggested you could use click events.

start();
end();
$('button').click(function () {
     start();
     end();
});

So the problem would be created automatically the first time and then the application would wait on an event to create a new problem..or whatever you are using it for.

Answer:2

I was furious when i could not figure out why my app was working fine in Chrome but not IE. After hours of looking at my back end making sure my JSON was coming through to each browser correctly, I ...

I was furious when i could not figure out why my app was working fine in Chrome but not IE. After hours of looking at my back end making sure my JSON was coming through to each browser correctly, I ...

I want scrollTop to trigger only once, so that the red box doesn't appear again after it has been closed with the button. I know there is .one(), but I have no idea how to apply it to my code. Below ...

I want scrollTop to trigger only once, so that the red box doesn't appear again after it has been closed with the button. I know there is .one(), but I have no idea how to apply it to my code. Below ...

Below is the code in which changeme is the text i want to change on the click of the button. HTML code and JS is below: 1) whatever user input in field will be capture and replace with changeme if ...

Below is the code in which changeme is the text i want to change on the click of the button. HTML code and JS is below: 1) whatever user input in field will be capture and replace with changeme if ...

  1. click button change text javascript
  2. click button change image javascript
  3. click button change color
  4. click button change background color css
  5. click button change text jquery
  6. click button change color css
  7. click button change image
  8. click button change color jquery

I'm looking to use a map to map a string to an integer value but allowing the int to be manipulated within the map. E.g. var Map : map<string, int>; Map["foo"] = 5; Map["foo"] = Map["foo"] * ...

I'm looking to use a map to map a string to an integer value but allowing the int to be manipulated within the map. E.g. var Map : map<string, int>; Map["foo"] = 5; Map["foo"] = Map["foo"] * ...