Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Listing things in a DIV with JS?

1. ## Listing things in a DIV with JS?

I've made a code that finds the factors of an inputted number. Here's the script:
Code:
```<html>
<title>Factoring With Modulus</title>
<style type="text/css">
body{font-family:Cambria;color:rgb(240,240,240)}
input[type=text]{width:100px;border:none;border-bottom:2px solid blue;font-family:Cambria;font-size:16px;text-align:center;background-color: transparent;color:rgb(240,240,240)}
button{border:2px dashed-red;background-color:#BBF}
var{background:tansparent;}
</style>
<body background="wallpaper2.jpg">
<h1>JavaScript: Factoring With Modulus</h1><br>
Input the number to factor.<br>
<input type="text" id="a" maxlength="7"><br><br>
<button onclick="factors()">GO</button><br><br>

<script>
function factors()
{
var a = document.getElementById("a").value;
document.write(a+"= <br>")
for (i = 0; i < (Math.sqrt(2*a)); i++)
if (a%i==0)
{
document.write("±"+i+"*"+"±"+a/i+"<br>")
}

if (a/(Math.abs(a))==-1)
{
for (i=0; i<(Math.sqrt(-2*a));i++)
if (a%i==0)
{
document.write(i+"*"+a/i+"<br>")
}
}
}
</script>

</body>
</html>```
However, when I factor it, It will go to a blank unformatted page to list the factors. How can I list the factors on the webpage itself like a div? I tried using a <div> and innerHTML but it only lists one factor.

• ## Try this ...

You cannot use 'document.write()' after the page has loaded.
Doing so causes the page to reload from scratch, hence the blank page.

Use: <div id='Factors'></div>
with multiple writes to the DIV element with
document.getElementById('Factors').innerHTML = 'xxx'; (whatever) to initialize and
document.getElementById('Factors').innerHTML += 'yyy'; to add to the element contents.

• Wow, somehow i think i knew that, but it just didn't click. Thanks so much, this really helped me.

• You're most welcome.