www.webdeveloper.com
Results 1 to 11 of 11

Thread: Why isn't this working

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Why isn't this working

    I don't understand why paragraph(demo) is not changing into "IT WORKS!!!"
    Here is the script:

    <html>
    <body>

    <script>

    function submitinfo()

    {
    var firstname = document.getElementById("firstname").value;
    var lastname = document.getElementById("lastname").value;
    var middlename = document.getElementById("middlename").value;

    alert("Hello " + firstname + " " + middlename + " " + lastname);

    if (middlename == null);
    {
    getElementById("demo").innerHTML= "IT WORKS!!!";
    }

    }

    </script>
    First Name: ....<input type="text" id="firstname">
    <br />
    Middle Name: <input type="text" id="middlename">
    <br />
    Last Name: ....<input type="text" id="lastname">
    <br />
    <input type="button" value="Submit" onclick="submitinfo()">
    <br />
    <br />
    <p id= "demo"></p>
    </body>
    </html>

  2. #2
    Join Date
    Mar 2012
    Posts
    4
    Try a

    Code:
    <span id="demo"> <span>
    I have an exact example of what you want give me a second to find it.#

    Also I wouldn't write the javascript in the same file as the HTML. It's a bad habit.

    Put this in your HTML Header.

    Code:
    <script type="text/javascript" src="javascript.js"></script>

    And simply open a new notepad++ and save a blank document as javascript.js and put all your js code in there.
    Last edited by Lucas1; 10-13-2012 at 01:25 PM.

  3. #3
    Join Date
    Nov 2010
    Posts
    1,086
    try it with
    Code:
    document.getElementById("demo").innerHTML= "IT WORKS!!!";

  4. #4
    Join Date
    Nov 2010
    Posts
    1,086
    Quote Originally Posted by Lucas1 View Post
    Also I wouldn't write the javascript in the same file as the HTML. It's a bad habit.
    actually it's a very good habit when posting questions here to include all the relevant information (rendered html, js and css) in the one code block. It saves people having to ask to see other bits and makes it easier to test your code. Once you have your code working you can do what you like with it.

    I've never been hugely swayed by the separation of concerns arguments. If you are working on large frameworks with multiple pages depending on the same external files, of course it makes sense. But to create a separate js file for a few lines of code seems unnecessarily dogmatic.

    What is important is not to put javascript in the head, either as an external file or a code block. It will slow down the loading of the page (as the script has to be read before the html is rendered) and make it necessary for any script accessing html elements on page load to be wrapped in a window onload function. The best place for javascript - either inline or as an external file reference - is just before the closing </body> tag

  5. #5
    Join Date
    Jan 2011
    Posts
    117
    What do you mean by :

    if (middlename == null);


    why null and what did you expect from that?!

    try to keep it simple and do

    if (!middlename);

    and you're on gear.

  6. #6
    Join Date
    Nov 2010
    Posts
    1,086
    not with
    getElementById("demo").innerHTML

    that will never work...

  7. #7
    Join Date
    Jan 2011
    Posts
    117
    seeing this: getElementById("demo").innerHTML= "IT WORKS!!!";}}</script>
    First Name: ....<input type="text" id="firstname">...

    I presumed he was shorting out the illustration of his problem and that on his original code stands a properly written document.getElementBy(id) instead.

  8. #8
    Join Date
    Jan 2011
    Posts
    117
    ....
    Last edited by Troy III; 10-13-2012 at 02:32 PM.

  9. #9
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    My answer to this would have been 'Use the error console", but I don't like to repeat myself too often.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  10. #10
    Join Date
    Jan 2011
    Posts
    117
    and how would that help against syntactically correct code but semantically wrong meaning of a chosen expression? nothing can guard you against your own laspus lingua.

  11. #11
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by Troy III View Post
    and how would that help against syntactically correct code but semantically wrong meaning of a chosen expression?
    It wouldn't; It wasn't.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

Thread Information

Users Browsing this Thread

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

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
  •  
HTML5 Development Center



Recent Articles