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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question HELP! for loops and document.write problem

    Hello, I have a problem with the following code!
    I want to use it so that customers check a checkbox under each photo they want, in a photo gallery, and when they finish, they can click and the photos they checked appear using document.write in a new page!
    The problem I have is that when the script finds the first image, it uses document.write, shows only the first image it finds and then ends the foor loop!
    How can I code it so that it finds all the photos first and then shows them in the new page!
    Here is the sample code, sorry for my bad English and thanks for your time!
    --------------------------------------------------------------------------

    <script type="text/javascript">
    function findphotos(test){
    for (i=0; i<document.test.checkgroup.length; i++){
    if (document.test.checkgroup[i].checked==true){
    document.write('<img src="'+i+'.jpg">');
    }
    }
    }
    </script>

    <form name="test">
    <input type="checkbox" name="checkgroup" />
    <input type="checkbox" name="checkgroup" />
    <input type="checkbox" name="checkgroup" />
    </form>
    <h1 onclick="javascript:findphotos(test)">Find</h1>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Each call to document.write() erases the page including the script. So it only works once.

    document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

    If you wish to pass variables to a new page you must use a query string or a cookie.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanx for your replay! If you have any reference about doing that I would appreciate it! Or if there is any other way to write at the same page instead of using document.write! Thanx again for your time!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by pagokoftis View Post
    Thanx for your replay! If you have any reference about doing that I would appreciate it! Or if there is any other way to write at the same page instead of using document.write! Thanx again for your time!
    Google is your friend! As is the search feature of this forum.

  • #5
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok! Thnx!


  •  

    Tags for this Thread

    Posting Permissions

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