The page keeps refreshing when I don't want the html to be updated in javascript


Spencer 1O1

I've been trying to fix this, but when innerhtml is set to some value, the page keeps refreshing. I've been working on this for a while now, but I can't figure it out. I've looked at other people's code, but can't figure it out myself.

Here is the code:

function myFunction(){
var primes = [];

function isPrime(num) {
  for(var i = 2; i <= num/2; i++)
    if(num % i === 0 ) return false;
    primes.push(num);

}

function listOfPrimes(min, max) {
	for(var j = min; j < max; j++)	
		isPrime(j);
}

	


	
var minimum = document.getElementById('Minimum').value;
var maximum = document.getElementById('Maximum').value;

listOfPrimes(minimum,maximum);// DO NOT ENTER DECIMALS!!! minimum and maximum numbers that you want.
primes = primes.filter(function(x){ return x > 1});
primes = primes.toString();


document.getElementById("Your_Primes").innerHTML = primes;

}

var button = document.querySelector("button");
          button.addEventListener("click", function() {
                myFunction();
          }); 
<!doctype html>

<html>
<head>
	<title>Prime Lister</title>
</head>
<body>

<form>

Minimum Number:<br>
<input type="number" placeholder="MinimumWholeNumber" id="Minimum" multiple="1" required><br><br>
Maximum Number:<br>
<input type="number" placeholder="MaximumWholeNumber" id="Maximum" multiple="1" required><br><br>
<button>Enter</button>
</form>


<p>Your Primes: <p id="Your_Primes"></p></p>
<script type="text/javascript" src="Web_Prime_Lister.js"></script>
</body>
</html>

Cory Kleiser

You have some serious problems with your JavaScript. I rewrote your script and attached it to this answer with the correct javascript:

var minimum;
var maximum;
var primes;

function myFunction(e) {
  e.preventDefault();
  minimum = document.getElementById('Minimum').value;
  maximum = document.getElementById('Maximum').value;
  primes = [];

  listOfPrimes(minimum, maximum); // DO NOT ENTER DECIMALS!!! minimum and maximum numbers that you want.  console.log(primes);
  primes = primes.filter(function(x) {
    return x > 1;
  });
  primes = primes.toString();
  document.getElementById("Your_Primes").innerHTML = primes;
}

function isPrime(num) {
  for (var i = 2; i <= num / 2; i++)
    if (num % i === 0) {
      return false;
    }
  primes.push(num);
}

function listOfPrimes(min, max) {
  for (var j = min; j < max; j++) {
    isPrime(j);
  }
}
<form>
  Minimum Number:<br>
  <input type="number" placeholder="MinimumWholeNumber" id="Minimum" multiple="1" required><br><br> Maximum Number:<br>
  <input type="number" placeholder="MaximumWholeNumber" id="Maximum" multiple="1" required><br><br>
  <button id="button" onclick="myFunction(event)">Enter</button>
</form>


<div>Your Primes:
  <p id="Your_Primes"></p>
</div>

I'll tell you exactly what's wrong with the code, but it's easier to tell you what I did. The basic logic you have in your function is correct, but you just arranged the logic incorrectly. First, I instantiate the variables you'll use between functions to expand the scope. Second, I set the myFunctionfunction that will be executed on click, inserted the function to execute the logic and find the prime number, and prevent the default event that forces the page to reload. Finally, I put your declaration outside the on click function inside so that every time the user changes the information and clicks the button, it updates all the information. These statements include getting min and max values, changing arrays to strings, and setting internal html. Hope this helps.

Related


html page keeps refreshing

Mohammad Tinwala | This is my admin login page code, but the page keeps refreshing instead of redirecting. I read a lot of articles but still can't solve the problem coding: <!DOCTYPE HTML> <HTML> <head> <title>Admin Login</title> <link rel="styleshe

Javascript, page keeps refreshing

kiss prox I'm trying to submit a form, but when I open the page it keeps refreshing without me submitting or doing anything <html> <head> </head> <body> <form id="someForm"> <input id="someInput" /><button>Send</button> </form> <script>

Don't get collection when refreshing page - AngularFire

false rune There is a problem refreshing the page, my call cannot be initialized in the constructor. I've been using this as a guide: https://www.youtube.com/watch?v=gUmItHaVL2w&ab_channel=TraversyMedia https://github.com/angular/angularfire edit: To make it w

Don't get collection when refreshing page - AngularFire

false rune There is a problem refreshing the page, my call cannot be initialized in the constructor. I've been using this as a guide: https://www.youtube.com/watch?v=gUmItHaVL2w&ab_channel=TraversyMedia https://github.com/angular/angularfire edit: To make it w

Don't get collection when refreshing page - AngularFire

false rune There is a problem refreshing the page, my call cannot be initialized in the constructor. I've been using this as a guide: https://www.youtube.com/watch?v=gUmItHaVL2w&ab_channel=TraversyMedia https://github.com/angular/angularfire edit: To make it w

Don't get collection when refreshing page - AngularFire

false rune There is a problem refreshing the page, my call cannot be initialized in the constructor. I've been using this as a guide: https://www.youtube.com/watch?v=gUmItHaVL2w&ab_channel=TraversyMedia https://github.com/angular/angularfire edit: To make it w

Don't get collection when refreshing page - AngularFire

false rune There is a problem refreshing the page, my call cannot be initialized in the constructor. I've been using this as a guide: https://www.youtube.com/watch?v=gUmItHaVL2w&ab_channel=TraversyMedia https://github.com/angular/angularfire edit: To make it w

Page keeps refreshing when Bootstrap Modal is clicked

Mark I have a page with two buttons connected to two different bootstrap modals. They were all working fine, but suddenly stopped. Now, whenever I click either button, the page stays refreshed instead of showing the corresponding modal. I don't know where the

Page keeps refreshing when Bootstrap Modal is clicked

Mark I have a page with two buttons connected to two different bootstrap modals. They were all working fine, but suddenly stopped. Now, whenever I click either button, the page stays refreshed instead of showing the corresponding modal. I don't know where the

Page keeps refreshing when Bootstrap Modal is clicked

Mark I have a page with two buttons connected to two different bootstrap modals. They were all working fine, but suddenly stopped. Now, whenever I click either button, the page stays refreshed instead of showing the corresponding modal. I don't know where the

Page keeps refreshing when Bootstrap Modal is clicked

Mark I have a page with two buttons connected to two different bootstrap modals. They were all working fine, but suddenly stopped. Now, whenever I click either button, the page stays refreshed instead of showing the corresponding modal. I don't know where the

Page keeps refreshing when Bootstrap Modal is clicked

Mark I have a page with two buttons connected to two different bootstrap modals. They were all working fine, but suddenly stopped. Now, whenever I click either button, the page stays refreshed instead of showing the corresponding modal. I don't know where the

Page keeps refreshing when using AJAX

Damian Anthony I have created a modal box that contains a form. Once submitted using ajax and php, the form will return the input and then the modal box should disappear. The problem is that although the results are displayed for a few seconds, then the box di

I don't want .html in website url

username My html page shows .html at the end of the url and I want to remove it. Can anyone help me. my url is like mysitedoain.com/about.html ..../contact.html Facebook facebook Hasanuzzaman Sattar to contact You need to create a file called ".htaccess" in t

I don't want .html in website url

username My html page shows .html at the end of the url and I want to remove it. Can anyone help me. my url is like mysitedoain.com/about.html ..../contact.html Facebook facebook Hasanuzzaman Sattar to contact You need to create a file called ".htaccess" in t

I don't want .html in website url

username My html page shows .html at the end of the url and I want to remove it. Can anyone help me. my url is like mysitedoain.com/about.html ..../contact.html Facebook facebook Hasanuzzaman Sattar to contact You need to create a file called ".htaccess" in t

I don't want .html in website url

username My html page shows .html at the end of the url and I want to remove it. Can anyone help me. my url is like mysitedoain.com/about.html ..../contact.html Facebook facebook Hasanuzzaman Sattar to contact You need to create a file called ".htaccess" in t

I don't want .html in website url

username My html page shows .html at the end of the url and I want to remove it. Can anyone help me. my url is like mysitedoain.com/about.html ..../contact.html Facebook facebook Hasanuzzaman Sattar to contact You need to create a file called ".htaccess" in t

I don't want .html in website url

username My html page shows .html at the end of the url and I want to remove it. Can anyone help me. my url is like mysitedoain.com/about.html ..../contact.html Facebook facebook Hasanuzzaman Sattar to contact You need to create a file called ".htaccess" in t

I don't want .html in website url

username My html page shows .html at the end of the url and I want to remove it. Can anyone help me. my url is like mysitedoain.com/about.html ..../contact.html Facebook facebook Hasanuzzaman Sattar to contact You need to create a file called ".htaccess" in t

I don't want .html in website url

username My html page shows .html at the end of the url and I want to remove it. Can anyone help me. my url is like mysitedoain.com/about.html ..../contact.html Facebook facebook Hasanuzzaman Sattar to contact You need to create a file called ".htaccess" in t