Making a field un-editable until the correct radio button is pressed

Discussion in 'Programming' started by liam1412, Oct 2, 2008.

  1. #1
    Hi

    I posted this a while back but can't find the thread and don't think anyone answered anyway.

    Basically I want a field to be greyed out until someone checks the premium listing radio button. I assume JS is the only way to do this.

    Please can anyone advise if it would be possible and how it would be done. Im a JS no hoper. I tried to learn it failed. He he
     
    liam1412, Oct 2, 2008 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    First use html to disabled the field, using disabled option.

    Then use JS onclick attribute to enable it, like:

    onclick="document.getElementById('disabledfieldname').disabled='false';"

    or something along those lines.

    Peace,
     
    Barti1987, Oct 2, 2008 IP
    liam1412 likes this.
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    Yup.

    Your HTML should be like this:

    
    <form id="mainForm" action="" method="post">
        ...
        <label>Enable Text Box <input onclick="enableBox('mainForm', 'textbox')" type="radio" name="boxEnabler" /></label>
        <label>Textbox <input type="text" name="textbox" />
        ...
    </form>
    <script type="text/javascript"><!--
        document.getElementById('mainForm').textbox.disabled='true';
    //--></script>
    
    Code (markup):
    Then your JS (don't have to have this as a separate function, just neater and reusable):

    
    function enableBox(formId, boxName){
     document.getElementById(formId).boxName.disabled='false';
    }
    
    Code (markup):
    That should work. What it does is writes the form in HTML, then disables the text box with JS (equivelant to using onload, but faster), so people without JS can still use it.

    That function will enable any input, select or textarea if you need it, but obviously it works fine with just one :p
     
    blueparukia, Oct 2, 2008 IP
    liam1412 likes this.
  4. liam1412

    liam1412 Active Member

    Messages:
    387
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Thanks to both of you. Works a treat.

    Rep added!!!!!!!
     
    liam1412, Oct 2, 2008 IP
  5. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #5
    or you could have the box not even appear until the radio buton is checked, triggering the elementbyid in a <div> so that Ajax then makes it pop out <SHWING> like magic ..almost.
     
    ezprint2008, Oct 2, 2008 IP