www.webdeveloper.com
+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2012
    Posts
    2

    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. #2
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,334
    Hi there vilhelm,

    and a warm welcome to these forums.

    Use "document.getElementById()" instead.

    The script would become...
    Code:
    if (document.Form.TypedInField.value=="The Cable Guy") {
        document.getElementById("foo").innerHTML="You just wrote a title of a nice movie"; }
    
    The HTML below the form element would be...
    Code:
    <div id="foo"></div>
    
    Change "foo" to suit your personal requirements.

    coothead

  3. #3
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    192
    Check it:
    HTML Code:
    <!doctype html>
    <html>
    <body>
        <fieldset>
            <legend>Enter a movie's name!</legend>
            <form name="Form">
                <input name="TypedInField" />
                <input type="button" value="Click me!" onclick="myFunction()" /> <label>e.g. The Cable Guy</label>
                <br/>
                <label id="output">&nbsp;</label>
            </form>
        </fieldset>
        <script>
            var myFunction = function () {
                var _output = document.getElementById('output');
                if (document.Form.TypedInField.value == '') {
                    alert('Please enter a movie name!');
                    document.Form.TypedInField.focus();
                } else if (document.Form.TypedInField.value == "The Cable Guy") { 
                    _output.innerHTML = "You just wrote the title of a nice movie!";
                } else {
                    _output.innerHTML = "Is this a good movie? I should watch it!";
                }
            }
        </script>
    </body>
    </html>
    "It will never rain roses: when we want to have more roses, we must plant more roses."
    - George Eliot

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

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



Recent Articles