JS-Noob needs help!

Discussion in 'HTML & Website Design' started by falcondriver, Mar 3, 2006.

  1. #1
    hi;

    i have a form with 2 fields:

    
    color:
    <select name="color">
    <option value=1>red</option>
    <option value=2>black</option>
    <option value=3>blue</option>
    <option value=4>white</option>
    <option value=3>yellow</option>
    </select>
    
    
    transparency:
    <input type="text" name="transparency" value="">
    
    Code (markup):
    i want to disable the transparency field if color is not 1, 3 or 4; could someone help me please?
     
    falcondriver, Mar 3, 2006 IP
  2. Slapyo

    Slapyo Well-Known Member

    Messages:
    266
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    I didn't test this, but it should work. If not it should point you in the right direction. Make sure to check the values of your options, I saw you had 2 options with value=3. Not sure if this was supposed to be like that, or if that 2nd value=3 was supposed to be value=5. Also, you will need to give your form the name attribute and change formname in the javascript to whatever you name your form. Hope this helps.

    <script language="javascript">
    	function chkColor(selObj) {
    		switch(selObj.options[selObj.selectedIndex].value) {
    			case 2:
    				formname.transparency.disabled = true;
    				break;
    			case 5:
    				formname.transparency.disabled = true;
    				break;
    		}
    	}
    </script>
    
    color:
    <select name="color" onchange="chkColor(this);">
    <option value=1>red</option>
    <option value=2>black</option>
    <option value=3>blue</option>
    <option value=4>white</option>
    <option value=5>yellow</option>
    </select>
    
    
    transparency:
    <input type="text" name="transparency" value="">
    Code (markup):
     
    Slapyo, Mar 15, 2006 IP
    falcondriver likes this.
  3. Slapyo

    Slapyo Well-Known Member

    Messages:
    266
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #3
    I forgot that in the code above it will only set the transparency field to disabled, but never switch back to enabled if 1, 3, or 4 are selected. This should fix that.

    <script language="javascript">
    	function chkColor(selObj) {
    		switch(selObj.options[selObj.selectedIndex].value) {
    			case 1:
    				formname.transparency.disabled = false;
    				break;
    			case 2:
    				formname.transparency.disabled = true;
    				break;
    			case 3:
    				formname.transparency.disabled = false;
    				break;
    			case 4:
    				formname.transparency.disabled = false;
    				break;
    			case 5:
    				formname.transparency.disabled = true;
    				break;
    		}
    	}
    </script>
    Code (markup):
     
    Slapyo, Mar 17, 2006 IP