Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.


Results 1 to 5 of 5

Thread: Disabling Form Parts

  1. #1

    Disabling Form Parts


    I don't do much Javascript work and I was hoping someone could help me debug the following code.

    I am looking to disable a form part (a file upload field) if they do not check off a checkbox.

    File Field Name: userfile
    Checkbox Name: editimage

    Here is the javascript

    HTML Code:
          function disablefield()
      // check everything you need to check and then
      if (document.forms[0].editimage.value == "yes"){
          document.forms[0].userfile.disabled = false;
    } else if (document.forms[0].editimage.value == "") {
    document.forms[0].userfile.disabled = true;
    Here is parts of the HTML form. The checkbox and the file field-

    HTML Code:
    <input onChange="disablefield(this);" name='editimage' type='checkbox' id='editimage' value='yes'>
    <input disabled='disabled' name='textfield' type='text' value='http://www.' size='13'>
    The javascript is placed in between the two.

    Please help!



  2. #2
    Backend Specialist
    I am not a javascript guru or anything, but I have done this before. For the checkbox value, you can use "document.forms[0].editimage.checked" and that will return a boolean value. so try the following:
      function disablefield() {
        if (document.forms[0].editimage.checked) {
          document.forms[0].userfile.disabled = ''; 
        } else { 
          document.forms[0].userfile.disabled = 'disabled'; 
    Then in your HTML, you don't have to pass in "this" to the function since you arent referencing anything within the code. So just do the "onChange='disablefield()'"


  3. #3
    Registered User
    HTML Code:
    function disableField(){
    	var p = document.forms[0];
    ... try that. the disabled property is true/false
    PHP Code:
    <zip> echo this; </zip

  4. #4
    Both worked, thanks so much! If anyone does read this, make sure that your field is disabled too. If not, then when the page loads initially, the field will be enabled.

    Thanks again!

  5. #5
    Registered User
    HTML Code:
    function disableField(){
          var p = document.forms[0];
    I don't know why I didnt think of this earlier ... brain fart
    PHP Code:
    <zip> echo this; </zip

Thread Information

Users Browsing this Thread

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

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012