dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: JavaScript-How to Disable Html Button by JavaScript

  1. #1
    Join Date
    Dec 2010
    Posts
    7

    JavaScript-How to Disable Html Button by JavaScript

    Hi Guys,

    I am new at JavaScript and start to do some tutorials.What I am trying to do here is prompting user to input a name and if the name was valid the page(document) will display with all objects like the button.But if user enter a wrong name then the button will be disabled!
    I create the following code but it did not work

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>New Web Project</title>
    <script language="JavaScript" type="">
    function changeColor(){
    document.bgColor = "Gray";
    }
    </script>
    </head>
    <body>
    <script language="JavaScript" type="text/javascript">
    var person = "";
    person = prompt('What is Your Name:');
    if (person == "Foo") {
    document.write("<h1 />Welcome " + person);
    document.bgColor = "Yellow";
    }
    else {
    document.write("<h1 />Access Denied!!!!");
    document.bgColor = "Red";
    document.getElementById("gree").disabled = true;
    }
    </script>

    <div>
    <p/><input id="gree" type="button" value="Gray " onClick="changeColor();">
    </div>
    </body>
    </html>

    as you can see I used the:
    document.getElementById("gree").disabled = true;
    but it did not work , could you please give an idea how I can solve this problem?
    Thanks

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    You have referred an element ( document.getElementById("gree")) before it was actually loaded, as the loading process is a sequential one, from top to bottom. You need to wrap your code into a function, and trigger that function window.onload

    On the other hand, document.write() is not a dynamic method. It will overwrite the document. You should use DOM methods to create and append new elements. Or even innerHTML method.

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