SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    check if checkbox is checked

    HTML Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(function(){
    	$("#widen").attr('disabled', true);
    	if ($('#ads').checked = true) {
    		alert('ok');// not work
    	}
    });
    </script>
    </head>
    
    <body>
    
    <div><input type="checkbox" id="ads" /><label for="ads"> Hide ads.</label></div>
    <div style="padding-left:20px">
    	<input type="checkbox" id="widen" /><label for="widen"> Widen message aera to fit window</label>
    </div>
    </body>
    </html>
    I don't know why my check function doesn't work. Can you see it for me ?

  2. #2
    SitePoint Member
    Join Date
    Sep 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    = is an assignment operator. Use ==

    if ($('#ads').checked == true)

    But to cut out the redundancy, just use:

    if ($('#ads').checked)

  3. #3
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure, derfleurer ? Both of yours dont work. Did you test it ?

  4. #4
    SitePoint Member
    Join Date
    Sep 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works for me. In fact, with the assignment operation, you'd get the value of the left operand anyway. There's an issue on your end that we aren't seeing. Try a regular alert outside of all of your jQuery code.

  5. #5
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    function alert() works fine when tested alone. Infact so far, every scripts I've worked on run just fine except this "check checkbox" function. So there wouldn't be js problem on browsers.

    Gosh, this is driving me nut, it took me 3 hour already.

  6. #6
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I finally got it work.

    HTML Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(function(){
    //	$("#widen").attr('disabled', true);
    //	if ($('#ads').checked == true) {
    //		alert('ok');// not work
    //	}
    	
    	ads = $('#ads');
    	ads.change(function(){
    	  if ($(this).attr('checked') == true)
    	  {
    			alert('god damn');
    	  }
    	});
    });
    </script>
    </head>
    
    <body>
    
    <input type="checkbox" id="ads" /><label for="ads"> Hide ads.</label>
    <input type="checkbox" id="widen"  /><label for="widen"> Widen message aera to fit window</label>
    </body>
    </html>
    What got in my chest is though, I still can't figure out why the first snippet works for every body but me.


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
  •