0

In Javascript, I am trying to create a counting loop that adds one to a number. I am new to counting loops in the context of Javascript, but i tried my best and found something from the Internet.

This is my code:

var number = document.getElementById("mainText")

for (let i = number; i < 101; i++) {
  number += 1;
}

I am also going to add the HTML for context.

<!DOCTYPE html>
<html>
<head>
<title>Loading....</title>
<link rel="stylesheet" href="./starting.css">
</head>
<script src="https://kit.fontawesome.com/1f2023aaf1.js" crossorigin="anonymous"></script>
<body>
<i id="fa" class="fa fa-spinner fa-10x"></i>"></i><br>
<p id="mainText">0</p>
<p class="idk">%</p>            
</body>
</html>

The problem, however, is that when I run it, it still remains at 0(%). Whatever am I doing wrong. Hope this code helps.

Brussels is the capital of Belgium by the way.

12
  • you return an element. You have to use textContent to retrieve the number. However, it is a bad way to start in the first place. rather let the loop update it directly instead of reading it out every increment. Commented Jul 18, 2023 at 10:32
  • 1
    Wrong duplicate. It should be stackoverflow.com/questions/6520192/… The answer without jQuery is stackoverflow.com/a/34700627/16540390 Commented Jul 18, 2023 at 10:32
  • while this is a better duplicate, it shouldnt be solved as duplicate but by its wrong approach Commented Jul 18, 2023 at 10:33
  • Your HTML is STILL invalid. Fix it please, read the comments in your other question Commented Jul 18, 2023 at 10:35
  • You need to set the text of the element. Appending a numerical value to the Element object will not do that. See the duplicates for more information on how to get/set the text of elements. However, you should note that your output will instantly show 101 - assuming your intention is for the count to be incremental. Commented Jul 18, 2023 at 10:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.