SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Disabling Help

  1. #1
    SitePoint Guru SharifTK's Avatar
    Join Date
    Jan 2004
    Location
    New York, NY
    Posts
    621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Disabling Help

    I am trying to do this:

    - If a certain option in a select field is chosen, a textbox in the form becomes disabled, and background color changes to #cccccc; Is that possible? Can it work in both Mozilla and IE?

    Here is the code I have but does not work:

    HTML Code:
    <select name="asds">
    <option value="123" onClick="this.form.sample.disabled = true;">123</option>
    </select>
    <textarea name="sample"></textarea>
    SK

  2. #2
    SitePoint Addict
    Join Date
    Jul 2000
    Posts
    201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IE seems to have problems with onclick for options. Try onChange instead:
    HTML Code:
    <form name="myForm">
    <select name="asds" onChange="if (this.value=='123') {document.myForm.sample.disabled = true} else {document.myForm.sample.disabled = false};">
    <option value="123">123</option>
    <option value="enable" selected>enable</option>
    </select>
    <textarea name="sample"></textarea>
    </form>
    This works in IE and Firefox.

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just do....
    Code:
    <select...onchange="sample.disabled=(this.value=='123')">
    Probably better to call a separate function if you want the color change as welll.
    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function asds_onchange(selval, oTextarea)
    {
    	var bWhich = (selval == '123');
    	oTextarea.disabled = bWhich;
    	oTextarea.style.background = (bWhich) ? '#cccccc' : '#ffffff';
    }
    
    onload = function()
    {
    	document.forms[0].elements.asds.selectedIndex = 0; //keeps things in sync
    }
    
    </script>
    </head>
    <body>
    <form>
    <select name="asds" onchange="return asds_onchange(options[selectedIndex].value,sample)">
    <option value="" selected="selected">- choose -</option>
    <option value="123">123</option>
    <option value="456">456</option>
    <option value="789">789</option>
    </select>
    <textarea name="sample">foo</textarea>
    </form>
    </body>
    </html>
    ::: certified wild guess :::


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
  •