SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Textfield and Button

    Hey!

    I was doing this in PHP but I'd like it in Javascript,

    Basically there is a Submit button next to a text field,
    I want the fieldbutton to be disabled until "YES" appears in the text box.

    PHP Code:
    if (isset($_POST['contact']) && $_POST['chk'] == 'YES') {

            <
    input disabled="true" type="submit" id="sub" value="Save Changes" />
            <
    input type="text" name="chk" value="" /> 
    I was thinking,

    Code JavaScript:
    <script type="text/javascript">
    if (value=="YES")
      {
    document.getElementById(submit)
    sub.disabled=false
    }
    </script>
    Im not sure if this is good,
    I also have JQuery but havent gotten into it

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <input disabled="disabled" type="submit" id="sub" name="sub" value="Save Changes" />
    <input type="text" name="chk" id="chk" value="" />
    
    <script type="text/javascript">
    // get references
    var chk = document.getElementById("chk");
    var sub = document.getElementById("sub");
    
    // attach handler
    chk.onchange = function(){
      sub.disabled = chk.value.toUpperCase() != "YES";
    };
    </script>

  3. #3
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hy I see what you did there, that's a nice way to shorten it.

    The button will not enable, Without the disabled=disabled in the button the script doesnt run, maybe its not getting the elements.

    It's getting the ID of chk but not assigned an HTML.ID but is given a name, I tried adding ID="chk" but it did nothinh (Unless JS does this backwards)



    Code HTML4Strict:
    <script type="text/javascript">
    // get references
    var chk = document.getElementById("chk");
    var sub = document.getElementById("sub");
     
    // attach handler
    chk.onchange = function(){
      sub.disabled = chk.value.toUpperCase() != "YES";
    };
    </script>
     
    </head>
    <body>
     
    <div id="container">
     
     
    <table style="display: block; width: 200px; margin: 10px;">
    <td>
    <span class="update">File Last Saved @ August 3, 2008 12:36:22pm</span>
            <form method="post" action="/edit/Home.php">
            <textarea name="home" cols="90" rows="30"><h1>Home</h1>
            </textarea>
     
            <input disabled="disabled" type="submit" id="sub" name="sub" value="Save Changes" />
            <input type="text" name="chk" value="" maxlength="3" />
            </form>
        </td></tr></table>
     
    </div>

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your textbox needs id="chk" as an attribute.

  5. #5
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was having trouble with that, but thanks (The ID i had tried I dont know what went wrong!)

    I used this, which is actually more practical my first idea wasnt too smart:

    Code JavaScript:
    <script language=javascript type=text/javascript>
    <!--
    function disableField() {
        document.editbox.sub.value='Save Changes';
        document.editbox.sub.disabled=true;
    }
    function enableField() {
        document.editbox.sub.disabled=false;
    }
    //-->
    </script>

    Code HTML4Strict:
    <input disabled="disabled" type="submit" id="sub" name="sub" value="Save Changes" />
    <input type="radio" name="yesno" value="yes" onclick="javascript:enableField();" />
     Yes
     
    / 
     
    <input type="radio" name="yesno" value="no" checked="checked" onclick="javascript:disableField();" />
     No


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
  •