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
    May 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile document.write alternative help to convert to innerHTML?

    Hi!
    I want the result of a fileld to appear bellow the form and not in a new blank page (witch happens when you have document.write?).
    I guess it has something to do with "innerHTML"?

    Can anyone of you help me to make the result of this IF to appear bellow the form?

    if (document.Form.TypedInField.value == "The Cable Guy")
    { document.write("You just wrote a title of a nice movie") }

    I have the form and everything works perfect, except of that the result of what the user have wrote (beacuse of docuemnt.write) land in blank window.

    I would be very happy of any of you could solve this for me

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    You have discovered that 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 (including the Javascript which called it).

    Code:
    <span id ="message"></span>
    
    
    <script type = "text/javascript">
    
    //...
    
    document.getElementById("message").innerHTML = "";  // clear any previous message
    if (document.Form.TypedInField.value.toLowerCase() == "the cable guy") {
    document.getElementById("message").innerHTML = "You just wrote a title of a nice movie";
    }
    
    //.....
    
    </script>
    Note that you need to convert the entry to lower case as otherwise the cable guy or The cable gUY will not match.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 05-12-2012 at 10:04 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    vilhelm (05-12-2012)

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile

    thanks! but I get file:///C:/Documents%20and%20Settings/Vilhelm/Skrivbord/test.html?TypedInField=the+cable+guy

    and a blank page with noting in... and in the browser says it is something wrong with the javascript...

    Where have I make a misstake do you think?

    Here is the whole code:

    <script language="JavaScript" type="text/javascript">

    function FindMovie()

    {

    document.getElementById("message").innerHTML = "";
    if (document.Form.TypedInField.value.toLowerCase() == "the cable buy")
    { document.getElementById("message").innerHTML = "You just wrote a title of a nice movie"; }

    document.getElementById("message").innerHTML = "";
    if (document.Form.TypedInField.value.toLowerCase() == "hulk")
    { document.getElementById("message").innerHTML = "Hulk is also a nice movie"; }

    else if (document.Form.TypedInField.value == "")
    { alert("You need to type one letter or more"); return false; }

    else
    { document.write("Nothing found"); return false; }
    }
    </script>


    <form action="test.html" name="Form" onsubmit="return FindMovie();">

    <input type="text" name="TypedInField">
    <input type="submit" name="Submit" value="Find Movie">

    </form>

    <span id ="message"></span>

  • #4
    New to the CF scene
    Join Date
    May 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile

    Now it works. Many, many thanks to Philip M and iBall

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by iBall View Post
    that's ok, happy to have helped
    Yet more fluff!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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