SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Listbox Validation

    Hi,
    I got a listbox and textfield, if i choose the second option in the listbox the nearby textfield should be disabled otherwise the nearby textfield should be enabled. Pls help.

    Balen

  2. #2
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How's this? This code disables the textbox for any option below the "- select one-" option. If you specifically need a certain option to trigger the disable, just use its index number for the condition.

    This code works in IE and FF, but FF actually changes the color of the box, where as IE doesn't. You may want to include code for changing the style of the input somehow so the user knows that it is disabled.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Listbox JS Test</title>
    <script language="javascript" type="text/javascript">
    function setTextfield(){
    	if(document.getElementById("mySelect").selectedIndex > 0){
    		document.getElementById("myTextfield").disabled = true;
    	}else{
    		document.getElementById("myTextfield").disabled = false;
    	}
    }
    </script>
    </head>
    <body>
    <p>
      <select name="select" id="mySelect" onChange="setTextfield();">
    	<option>- select one -</option>
    	<option>Option 1</option>
    	<option>Option 2</option>
    	<option>Option 3</option>
    	<option>Option 4</option>
      </select>
    </p>
    <p>
      <input type="text" name="textfield" id="myTextfield">
    </p>
    </body>
    </html>


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
  •