Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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>
    <head>
    <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>
    </head>
    <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.

    Thanks for your help.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb 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.

  • #3
    New Coder
    Join Date
    May 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Wow, somehow i think i knew that, but it just didn't click. Thanks so much, this really helped me.
    Last edited by CrackTheAir; 05-27-2009 at 04:44 PM. Reason: SOLVED

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts
    You're most welcome.
    Glad I could help.
    Good Luck!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •