By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 711 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

code doesn't work

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



Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6.  
  7.  
  8. </head>
  9.  
  10. <body>
  11. <input id="agree" type="checkbox" />I agree<br />
  12. <input type="button" id="continue" value="continue" disabled="disabled">
  13. <script type="text/javascript" src="js/jQuery.js"></script>
  14. <script>
  15. $(document).ready(function(){
  16.     $('#agree').change(function(){
  17.              state = $(this).attr('value');
  18.             //alert(state);
  19.             if(state == 'on')
  20.             {
  21.                 $('#continue').removeAttr('disabled');
  22.             }
  23.             else if(state=='')
  24.             {
  25.                 $('#continue').attr('disabled','disabled');
  26.             }
  27.     });    
  28. });
  29. </script>
  30. </body>
  31. </html>
  32.  
Nov 5 '13 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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.
Nov 5 '13 #2

P: 13
ok thanks..
but after unchecking the checkbox it was not going to its original state that is disabled the button
Nov 5 '13 #3

Dormilich
Expert Mod 5K+
P: 8,639
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.
Nov 5 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.