code doesn't work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dhaval1993
    New Member
    • Apr 2013
    • 13

    code doesn't work

    this code does not work for me.I want to enable button only if checkbox is on.
    please help.



    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    
    </head>
    
    <body>
    <input id="agree" type="checkbox" />I agree<br />
    <input type="button" id="continue" value="continue" disabled="disabled">
    <script type="text/javascript" src="js/jQuery.js"></script>
    <script>
    $(document).ready(function(){
    	$('#agree').change(function(){
    			 state = $(this).attr('value');
    			//alert(state);
    			if(state == 'on')
    			{
    				$('#continue').removeAttr('disabled');
    			}
    			else if(state=='')
    			{
    				$('#continue').attr('disabled','disabled');
    			}
    	});	
    });
    </script>
    </body>
    </html>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    don’t use attr('value'). that receives the initial state of the checkbox (i.e. JS’s .defaultValue property). ref.

    use the .val() method instead.

    tbh, plain JavaScript would be better/sufficient here.

    Comment

    • dhaval1993
      New Member
      • Apr 2013
      • 13

      #3
      ok thanks..
      but after unchecking the checkbox it was not going to its original state that is disabled the button

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        but after unchecking the checkbox it was not going to its original state that is disabled the button
        can you elaborate that? I have no idea what that means.

        Comment

        Working...