checkbox

Discussion in 'JavaScript' started by connieyongg, Sep 26, 2006.

  1. #1
    Hi guys,

    Lets consider if i were have 5 checkbox and a disabled submit button, how could i enable the submit button with at least one checkbox being ticked?

    Thank you in advance and have a nice day.
     
    connieyongg, Sep 26, 2006 IP
  2. VONRAT

    VONRAT Banned

    Messages:
    181
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <script>
    	var selected = 0;
    	function EnableDisable(chk,sel) {
    		var retVal = sel;
    		if ( chk.checked ) {
    			retVal++;
    		} else {
    			retVal--;
    		}
    		alert( chk.checked );
    		var o = document.getElementById("btnSubmit");
    		
    		if ( retVal > 0 ) {
    			o.disabled = "";
    		} else {
    			o.disabled = "disabled";
    		}
    		
    		return retVal;
    	}
    </script>
    
    <form id="frmTest">
    	<input id="chk1" type="checkbox" value="1" onclick="selected = EnableDisable(this,selected)" />
    	<input id="chk2" type="checkbox" value="1" onclick="selected = EnableDisable(this,selected)"  />
    	<input id="chk3" type="checkbox" value="1" onclick="selected = EnableDisable(this,selected)"  />
    	<input id="btnSubmit" type="submit" value="Submit" disabled="disabled"/>
    </form>
    
    HTML:
     
    VONRAT, Sep 27, 2006 IP
  3. connieyongg

    connieyongg Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you for your kind advice and have a nice day
     
    connieyongg, Sep 27, 2006 IP
  4. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi!

    Remember the first thing you should consider when scripting in Javascript is how the page will behave if JS capability is not present. In this case, the user won't be able to submit the form, because you've disabled the submit button.

    I suggest you attach a load event listener, and disable the submit button through Javascript. That way, the JS and non-JS environments do not clash with each other.

    - P
     
    penagate, Sep 27, 2006 IP