SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checkbox disable

    I want to keep a checkbox disabled when page loads.

    below to the checkbox I want to keep a button with label "Enable" . When clicked this will enable the checkbox.


    My question:

    <input type="checkbox" name="chkbox" disabled value="">

    <input type="button" name="button" value="enable" onclick="callMe();">



    when clicking on the button I want to write a javascript callMe() function which will enable this checkbox.

    Can I write this way document.forms[0].chkbox.disable=false;

    Is this the proper way to code this ?

  2. #2
    SitePoint Enthusiast David.A's Avatar
    Join Date
    May 2010
    Location
    Milky Way Galaxy
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Although that could work, it could be dangerous should [0] be the wrong element. Add an ID to the <form> and then do something like:

    Code:
    document.getElementById('form').chkbox.disabled=false;

  3. #3
    Follow: @AlexDawsonUK silver trophybronze trophy AlexDawson's Avatar
    Join Date
    Feb 2009
    Location
    England, UK
    Posts
    8,111
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    The default method to disable a form element in HTML uses disabled attribute with a value of "disabled" so you would need...

    document.getElementById('form').chkbox.disabled="disabled"

  4. #4
    SitePoint Enthusiast David.A's Avatar
    Join Date
    May 2010
    Location
    Milky Way Galaxy
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Either works, I perfer true/false for readability, and it's shorter.

    Here is a quick example showing that both work:
    Code:
    <form action="">
    <input type="checkbox" disabled="disabled" id="chkbox" />
    <div onmouseover="document.getElementById('chkbox').disabled=false" 
         onmouseout="document.getElementById('chkbox').disabled='disabled'">
         HOVER me to change checkbox
    </div>
    </form>


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
  •