View Full Version : Listing things in a DIV with JS?

05-27-2009, 04:48 PM
I've made a code that finds the factors of an inputted number. Here's the script:

<title>Factoring With Modulus</title>
<style type="text/css">
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}
<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>

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)

if (a/(Math.abs(a))==-1)
for (i=0; i<(Math.sqrt(-2*a));i++)
if (a%i==0)


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.

Thanks for your help.

05-27-2009, 05:14 PM
You cannot use 'document.write()' after the page has loaded.
Doing so causes the page to reload from scratch, hence the blank page. :eek:

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.

05-27-2009, 05:42 PM
Wow, somehow i think i knew that, but it just didn't click. Thanks so much, this really helped me.

05-28-2009, 12:24 AM
You're most welcome.
Glad I could help.
Good Luck!